<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                > 編寫:[kesenhoo](https://github.com/kesenhoo) - 原文:[http://developer.android.com/training/volley/requestqueue.html](http://developer.android.com/training/volley/requestqueue.html) 前一節課演示了如何使用`Volley.newRequestQueue`這一簡便的方法來建立一個`RequestQueue`,這是利用了Volley默認的優勢。這節課會介紹如何顯式的建立一個RequestQueue,以便滿足你自定義的需求。 這節課同樣會介紹一種推薦的實現方式:創建一個單例的RequestQueue,這使得RequestQueue能夠持續保持在你的app的生命周期中。 ### 1)Set Up a Network and Cache 一個RequestQueue需要兩部分來支持它的工作:一部分是網絡操作,用來傳輸請求,另外一個是用來處理緩存操作的Cache。在Volley的工具箱中包含了標準的實現方式:`DiskBasedCache`提供了每個文件與對應響應數據一一映射的緩存實現。 `BasicNetwork`提供了一個網絡傳輸的實現,連接方式可以是[AndroidHttpClient](http://developer.android.com/reference/android/net/http/AndroidHttpClient.html) 或者是 [HttpURLConnection](http://developer.android.com/reference/java/net/HttpURLConnection.html). `BasicNetwork`是Volley默認的網絡操作實現方式。一個BasicNetwork必須使用HTTP Client進行初始化。這個Client通常是AndroidHttpClient 或者 HttpURLConnection: - 對于app target API level低于API 9(Gingerbread)的使用AndroidHttpClient。在Gingerbread之前,HttpURLConnection是不可靠的。對于這個的細節,請參考[Android's HTTP Clients](http://android-developers.blogspot.com/2011/09/androids-http-clients.html)。 - 對于API Level 9以及以上的,使用HttpURLConnection。 為了創建一個能夠執行在所有Android版本上的應用,你可以通過檢查系統版本選擇合適的HTTP Client。例如: ~~~ HttpStack stack; ... // If the device is running a version >= Gingerbread... if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { // ...use HttpURLConnection for stack. } else { // ...use AndroidHttpClient for stack. } Network network = new BasicNetwork(stack); ~~~ 下面的代碼片段演示了如何一步步建立一個RequestQueue: ~~~ RequestQueue mRequestQueue; // Instantiate the cache Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap // Set up the network to use HttpURLConnection as the HTTP client. Network network = new BasicNetwork(new HurlStack()); // Instantiate the RequestQueue with the cache and network. mRequestQueue = new RequestQueue(cache, network); // Start the queue mRequestQueue.start(); String url ="http://www.myurl.com"; // Formulate the request and handle the response. StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String response) { // Do something with the response } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // Handle error } }); // Add the request to the RequestQueue. mRequestQueue.add(stringRequest); ... ~~~ 如果你僅僅是想做一個單次的請求并且不想要線程池一直保留,你可以通過使用在前面一課:[發送一個簡單的請求(Sending a Simple Request)](#)文章中提到`Volley.newRequestQueue()`方法在任何需要的時刻創建RequestQueue,然后在你的響應回調里面執行`stop()`方法來停止操作。但是更通常的做法是創建一個RequestQueue并設置為一個單例。下面將演示這種做法。 ### 2)Use a Singleton Pattern 如果你的程序需要持續的使用網絡,更加高效的方式應該是建立一個RequestQueue的單例,這樣它能夠持續保持在整個app的生命周期中。你可以通過多種方式來實現這個單例。推薦的方式是實現一個單例類,里面封裝了RequestQueue對象與其他Volley的方法。另外一個方法是繼承Application類,并在`Application.OnCreate()`方法里面建立RequestQueue。但是這個方法是不推薦的。因為一個static的單例能夠以一種更加模塊化的方式提供同樣的功能。 一個關鍵的概念是RequestQueue必須和Application context所關聯的。而不是[Activity](# "An activity represents a single screen with a user interface.")的context。這確保了RequestQueue在你的app生命周期中一直存活,而不會因為[activity](# "An activity represents a single screen with a user interface.")的重新創建而重新創建RequestQueue。(例如,當用戶旋轉設備時)。 下面是一個單例類,提供了RequestQueue與ImageLoader的功能: ~~~ private static MySingleton mInstance; private RequestQueue mRequestQueue; private ImageLoader mImageLoader; private static Context mCtx; private MySingleton(Context context) { mCtx = context; mRequestQueue = getRequestQueue(); mImageLoader = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache() { private final LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>(20); @Override public Bitmap getBitmap(String url) { return cache.get(url); } @Override public void putBitmap(String url, Bitmap bitmap) { cache.put(url, bitmap); } }); } public static synchronized MySingleton getInstance(Context context) { if (mInstance == null) { mInstance = new MySingleton(context); } return mInstance; } public RequestQueue getRequestQueue() { if (mRequestQueue == null) { // getApplicationContext() is key, it keeps you from leaking the // Activity or BroadcastReceiver if someone passes one in. mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext()); } return mRequestQueue; } public <T> void addToRequestQueue(Request<T> req) { getRequestQueue().add(req); } public ImageLoader getImageLoader() { return mImageLoader; } } ~~~ 下面演示了利用單例類來執行RequestQueue的操作: ~~~ // Get a RequestQueue RequestQueue queue = MySingleton.getInstance(this.getApplicationContext()). getRequestQueue(); ... // Add a request (in this example, called stringRequest) to your RequestQueue. MySingleton.getInstance(this).addToRequestQueue(stringRequest); ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看