android volley框架 2017 官方版

  android volley是架 款這鋪開android開發的時候,必須要鋪開使用的官方或者是不可或缺的重要框架;如果您想要鋪開快速的開發或者方便快捷的使用,就需要使用這款android volley框架了,架 這款軟件的官方操作非常的簡易,使用方便  ,架 是官方深空之眼辅助模式款非常值得信賴的軟件 ,需要的架 摯友趕快將軟件android volley框架下載來鋪開使用試試吧 ,相信您一定不會後悔的官方!

軟件功能

  自動調度網絡請求;

  高並發網絡接合;

  通過標準的架  HTTP cache coherence(高速緩存一致性)緩存磁盤和內存透明的感謝;

  擁穿著指定請求的優先級;

  網絡請求cancel機製。我們可以取消單個請求,官方或者指定取消請求隊列中的架 一個地方;

  框架輕易被定製,例如,官方深空之眼辅助刻印效果能叠加吗定製重試或者回退功能;

  包含了調試與追蹤工具;

  Volley 不適合用來下載大的架 數據文件。因為 Volley 會保持在解析的官方過程中所有的感謝。對於下載大量的架 數據操作 ,請思索使用 DownloadManager 。

軟件特色

  Volley:Volley 對外裸露的 API ,通過 newRequestQueue(…) 函數新建並打開一個請求隊列RequestQueue。

  Request :表示一個請求的抽象類。StringRequest  、JsonRequest 、ImageRequest都是它的子類 ,表示某種類型的深空之眼辅助刻印效果能叠加請求 。

  RequestQueue:表示請求隊列,裏麵包含一個CacheDispatcher(用於籌備行緩存請求的調度線程)、NetworkDispatcher數組(用於籌備行網絡請求的調度線程)  ,一個ResponseDelivery(返回結果分發接口) ,通過 start() 函數打開時會打開CacheDispatcher和NetworkDispatchers 。

  CacheDispatcher:一個線程 ,用於調度籌備行緩存的請求。打開後會不斷從緩存請求隊列中取請求籌備 ,隊列為空則等待 ,請求籌備落成則將結果傳遞給ResponseDelivery去執行後續籌備 。當結果未緩存過、深空之眼辅助模块解锁中的属性强化总等级45什么意思緩存失效或緩存需要刷新的情況下,該請求都需要重新進入NetworkDispatcher去調度籌備  。

  NetworkDispatcher:一個線程,用於調度籌備行網絡的請求。打開後會不斷從網絡請求隊列中取請求籌備,隊列為空則等待 ,請求籌備落成則將結果傳遞給ResponseDelivery去執行後續籌備,並判斷結果是否要鋪開緩存。

  ResponseDelivery  :返回結果分發接口,目前隻有基於ExecutorDelivery的在入參 handler 對應線程內鋪開分發 。

  HttpStack:籌備 Http 請求 ,深空之眼兑换码永久返回請求結果。目前 Volley 中有基於 HttpURLConnection 的HurlStack和 基於 Apache HttpClient 的HttpClientStack 。

  Network :調用HttpStack籌備請求,並將結果轉換為可被ResponseDelivery籌備的NetworkResponse 。

  Cache:緩存請求結果,Volley 默認使用的是基於 sdcard 的DiskBasedCache 。NetworkDispatcher得到請求結果後判斷是否需要存儲在 Cache ,CacheDispatcher會從 Cache 中取緩存結果。

使用會談明

  Volley籌備原理圖如下 :

  使用Volley框架實現網絡數據請求主要有以下三個步驟:

  1.創建RequestQueue對象,定義網絡請求隊列;

  2.創建XXXRequest對象(XXX代表String,JSON,Image等等) ,定義網絡數據請求的詳細過程;

  3.把XXXRequest對象增補到RequestQueue中 ,起始執行網絡請求。

  3.1 創建RequestQueue對象

  一般而言,網絡請求隊列都是整個APP內使用的全局性對象 ,因此最好寫入Application類中 :

  >public class MyApplication extends Application{

  // 建立請求隊列

  public static RequestQueue queue;

  @Override

  public void onCreate() {

  super.onCreate();

  queue = Volley.newRequestQueue(getApplicationContext());

  }

  public static RequestQueue getHttpQueue() {

  return queue;

  }

  }

  這是 ,我們還需要修改AndroidManifest.xml文件,使APP的Application對象為我們剛定義的MyApplication,並增補INTERNET權限:

  >

  android:name=".MyApplication"

  android:allowBackup="true"

  android:icon="@mipmap/ic_launcher"

  android:label="@string/app_name"

  android:supportsRtl="true"

  android:theme="@style/AppTheme" >

  3.2 創建XXXRequest對象並增補到請求隊列中

  Volley提供了JsonObjectRequest 、JsonArrayRequest 、StringRequest等Request形式 :

  JsonObjectRequest:返回JSONObject對象;

  JsonArrayRequest:返回JsonArray對象;

  StringRequest :返回String。

  另外可以繼承Request自定義Request。

  >public class MainActivity extends AppCompatActivity {

  @Override

  protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

  // GET請求

  VolleyGet();

  // POST請求

  VolleyPost();

  }

  // 定義POST請求的計劃

  private void VolleyPost() {

  // 請求地址

  String url = "http://ce.sysu.edu.cn/hope/";

  // 創建StringRequest ,定義字符串請求的請求方式為POST  ,

  StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener() {

  // 請求大捷後執行的函數

  @Override

  public void onResponse(String s) {

  // 打印出POST請求返回的字符串

  Toast.makeText(MainActivity.this, "POST: " + s, Toast.LENGTH_LONG).show();

  }

  }, new Response.ErrorListener() {

  // 請求出局時執行的函數

  @Override

  public void onErrorResponse(VolleyError volleyError) {

  }

  }){

  // 定義請求數據

  @Override

  protected Map getParams() throws AuthFailureError {

  Map hashMap = new HashMap();

  hashMap.put("phone", "11111");

  return hashMap;

  }

  };

  // 設置該請求的標簽

  request.setTag("abcPost");

  // 將請求增補到隊列中

  MyApplication.getHttpQueue().add(request);

  }

  // 定義GET請求的計劃

  private void VolleyGet() {

  // 定義請求地址

  String url = "http://ce.sysu.edu.cn/hope/";

  StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener() {

  @Override

  public void onResponse(String s) {

  // 打印出GET請求返回的字符串

  Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show();

  }

  }, new Response.ErrorListener() {

  @Override

  public void onErrorResponse(VolleyError volleyError) {

  }

  });

  // 設置該請求的標簽

  request.setTag("abcGet");

  // 將請求增補到隊列中

  MyApplication.getHttpQueue().add(request);

  }

  }

  輸出 :

  >// POST請求

  POST : <厚樸網站首頁源代碼 。。。>

  // GET請求

  GET   : <厚樸網站首頁源代碼 。 。。>

  3.3 隔絕請求

  3.3.1 隔絕特定標簽的網絡請求 :

  >// 網絡請求標簽為"abcGet"

  public void onStop() {

  super.onStop();

  MyApplication.getHttpQueues.cancelAll("abcGet");

  }

  3.3.2 取消這個隊列裏的所有請求:

  在activity的onStop()計劃裏麵 ,取消所有的包含這個tag的請求任務 。

  >@Override

  protected void onStop() {

  super.onStop();

  mRequestQueue.cancelAll(this);

  }

  四. GET和POST請求工具庫的封裝

  4.1 重寫Application

  因為網絡請求隊列相對於APP應用老會談是全局對象 ,因此可以定義在全局中。為此,我們新建一個LIMSApplication,並讓其繼承自Application 。

  LIMSApplication.java文件  :

  >public class LIMSApplication extends Application {

  public static RequestQueue volleyQueue;

  @Override

  public void onCreate() {

  super.onCreate();

  /* Volley配置 */

  // 建立Volley的Http請求隊列

  volleyQueue = Volley.newRequestQueue(getApplicationContext());

  }

  // 開放Volley的HTTP請求隊列接口

  public static RequestQueue getRequestQueue() {

  return volleyQueue;

  }

  }

  不要忘記在AndroidManifest.xml文件中修改Application的name和相應的網絡請求權限 :

  >

  android:name=".LIMSApplication"

  android:allowBackup="true"

  android:icon="@mipmap/ic_launcher"

  android:label="@string/app_name"

  android:supportsRtl="true"

  android:theme="@style/AppTheme" >

  4.2 GET和POST請求的封裝 :

  目前 ,VolleyRequestUtil工具庫隻包含了兩個函數 ,分別得到GET和POST請求 。

  VolleyRequestUtil.java:

  >public class VolleyRequestUtil {

  public static StringRequest stringRequest;

  public static Context context;

  /

*

  * 得到GET請求內容

  * 參數:

  * context :當前上下文;

  * url:請求的url地址;

  * tag:當前請求的標簽;

  * volleyListenerInterface :VolleyListenerInterface接口;

  * */

  public static void RequestGet(Context context, String url, String tag, VolleyListenerInterface volleyListenerInterface) {

  // 清除請求隊列中的tag標記請求

  LIMSApplication.getRequestQueue().cancelAll(tag);

  // 創建當前的請求 ,得到字符串內容

  stringRequest = new StringRequest(Request.Method.GET, url, volleyListenerInterface.responseListener(), volleyListenerInterface.errorListener());

  // 為當前請求增補標記

  stringRequest.setTag(tag);

  // 將當前請求增補到請求隊列中

  LIMSApplication.getRequestQueue().add(stringRequest);

  // 重啟當前請求隊列

  LIMSApplication.getRequestQueue().start();

  }

  /

*

  * 得到POST請求內容(請求的代碼為Map)

  * 參數:

  * context :當前上下文;

  * url:請求的url地址;

  * tag:當前請求的標簽;

  * params :POST請求內容;

  * volleyListenerInterface:VolleyListenerInterface接口;

  * */

  public static void RequestPost(Context context, String url, String tag, final Map params, VolleyListenerInterface volleyListenerInterface) {

  // 清除請求隊列中的tag標記請求

  LIMSApplication.getRequestQueue().cancelAll(tag);

  // 創建當前的POST請求 ,並將請求內容寫入Map中

  stringRequest = new StringRequest(Request.Method.POST, url, volleyListenerInterface.responseListener(), volleyListenerInterface.errorListener()){

  @Override

  protected Map getParams() throws AuthFailureError {

  return params;

  }

  };

  // 為當前請求增補標記

  stringRequest.setTag(tag);

  // 將當前請求增補到請求隊列中

  LIMSApplication.getRequestQueue().add(stringRequest);

  // 重啟當前請求隊列

  LIMSApplication.getRequestQueue().start();

  }

  }

  4.3 Volley請求(大捷或出局)的監細聽事件封裝 :

  封裝Volley請求(大捷或出局)的監細聽事件,見VolleyListenerInterface.java :

  >public abstract class VolleyListenerInterface {

  public Context mContext;

  public static Response.Listener mListener;

  public static Response.ErrorListener mErrorListener;

  public VolleyListenerInterface(Context context, Response.Listener listener, Response.ErrorListener errorListener) {

  this.mContext = context;

  this.mErrorListener = errorListener;

  this.mListener = listener;

  }

  // 請求大捷時的回調函數

  public abstract void onMySuccess(String result);

  // 請求出局時的回調函數

  public abstract void onMyError(VolleyError error);

  // 創建請求的事件監細聽

  public Response.Listener responseListener() {

  mListener = new Response.Listener() {

  @Override

  public void onResponse(String s) {

  onMySuccess(s);

  }

  };

  return mListener;

  }

  // 創建請求出局的事件監細聽

  public Response.ErrorListener errorListener() {

  mErrorListener = new Response.ErrorListener() {

  @Override

  public void onErrorResponse(VolleyError volleyError) {

  onMyError(volleyError);

  }

  };

  return mErrorListener;

  }

  }

  4.3 Volley圖片加載庫的封裝 :

  Volley庫還具有圖片加載的功能。但適合小圖片的異步加載 ,不適合於比較大的圖片資源的請求。

  Volley提供了多種Request計劃 ,譬如ImageRequest、ImageLoader、NetWorkImageView。

  網絡圖片資源的請求封裝如下:

  ImageLoaderUtil.java :

  >public class ImageLoaderUtil {

  /

*

  * 通過ImageRequest來顯示網絡圖片

  * */

  public static void setImageRequest(String url, final ImageView imageView) {

  ImageRequest imageRequest = new ImageRequest(url, new Response.Listener() {

  @Override

  public void onResponse(Bitmap bitmap) {

  imageView.setImageBitmap(bitmap);

  }

  }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {

  @Override

  public void onErrorResponse(VolleyError volleyError) {

  imageView.setBackgroundResource(R.mipmap.ic_launcher);

  }

  });

  LIMSApplication.getRequestQueue().add(imageRequest);

  }

  /

*

  * 通過ImageLoader來顯示網絡圖片

  * */

  public static void setImageLoader(String url, ImageView imageView, int defaultImageResId, int errorImageResId) {

  ImageLoader loader = new ImageLoader(LIMSApplication.getRequestQueue(), new BitmapCache());

  ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(imageView, defaultImageResId, errorImageResId);

  loader.get(url, imageListener);

  }

  /

*

  * 通過Volley的NetWorkImageView來顯示網絡圖片

  * */

  public static void setNetWorkImageView(String url, NetworkImageView netWorkImageView, int defaultImageResId, int errorImageResId) {

  ImageLoader loader = new ImageLoader(LIMSApplication.getRequestQueue(), new BitmapCache());

  netWorkImageView.setDefaultImageResId(defaultImageResId);

  netWorkImageView.setErrorImageResId(errorImageResId);

  netWorkImageView.setImageUrl(url, loader);

  }

  }

  五. VolleyRequestUtil與ImageLoaderUtil的使用

  5.1 用GET方式請求網絡資源:

  >new VolleyRequestUtil().RequestGet(this, "http://ce.sysu.edu.cn/hope/", "hopePage",

  new VolleyListenerInterface(this, VolleyListenerInterface.mListener, VolleyListenerInterface.mErrorListener) {

  // Volley請求大捷時調用的函數

  @Override

  public void onMySuccess(String result) {

  Toast.makeText(this, s, Toast.LENGTH_LONG).show();

  }

  // Volley請求出局時調用的函數

  @Override

  public void onMyError(VolleyError error) {

  // ...

  }

  });

  輸出:厚樸網站首頁的源代碼。

  5.2 用POST方式請求網絡資源  :

  >new VolleyRequestUtil().RequestPOST(this, "http://ce.sysu.edu.cn/hope/", "hopePage",

  new VolleyListenerInterface(this, VolleyListenerInterface.mListener, VolleyListenerInterface.mErrorListener) {

  // Volley請求大捷時調用的函數

  @Override

  public void onMySuccess(String result) {

  Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();

  }

  // Volley請求出局時調用的函數

  @Override

  public void onMyError(VolleyError error) {

  // ...

  }

  });

  輸出  :厚樸網站首頁的源代碼 。

  5.3 通過ImageRequest來顯示網絡圖片:

  >// 參數分別為:請求圖片的地址、圖片的容器ImageView

  ImageView imgView = (ImageView) findViewById(R.id.imgView);

  new ImageLoaderUtil().setImageRequest("http://7xinb0.com1.z0.glb.clouddn.com/skin/HopeRebuild/dist/images/logo/logo_40.png", imgView);

  在布局文件中,定義ImageView:

  >

  android:id="@+id/imgView"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content" />

  5.4 通過ImageLoader來顯示網絡圖片:

  >// 參數分別為:請求圖片的地址 、圖片的容器ImageView、默認顯示的圖片ResourceID 、請求出局時顯示的圖片的ResourceID

  new ImageLoaderUtil().setImageLoader("http://7xinb0.com1.z0.glb.clouddn.com/skin/HopeRebuild/dist/images/logo/logo_40.png", imgView, R.mipmap.default, R.mipmap.error);

  在布局文件中  ,定義ImageView :

  >

  android:id="@+id/imgView"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content" />

  5.5 通過Volley的NetWorkImageView來顯示網絡圖片 :

  >// 參數分別為:請求圖片的地址、圖片的容器NetworkImageView 、默認顯示的圖片ResourceID、請求出局時顯示的圖片的ResourceID

  NetworkImageView netWorkImageView = (NetworkImageView) findViewById(R.id.imgNetworkView);

  new ImageLoaderUtil().setNetWorkImageView("http://7xinb0.com1.z0.glb.clouddn.com/skin/HopeRebuild/dist/images/logo/logo_40.png", netWorkImageView, R.mipmap.default, R.mipmap.error);

  在布局文件中,定義NetworkImageView:

  >

  android:id="@+id/imgNetworkView"

  android:layout_width="300dp"

  android:layout_height="300dp"

  android:layout_centerHorizontal="true"/>

熱門資訊
上一篇:戀與深空祁煜定向軌道54層怎麽過 定向軌道祁煜54層通關分享
下一篇:快速搭建雲服務器短期實例  :無需長期訂閱 ,靈活變現!