Android中關於Volley的使用(五)從RequestQueue開始來深入認識Volley


在前面的幾篇文章中,我們學習了如何用Volley去網絡加載JSON數據,如何利用ImageRequest和NetworkImageView去網絡加載數據,而關於Volley的使用,我們都是從下面一行代碼開始的:

  

  1. Volley.newRequestQueue(this);  


這是Volley類創建了一個RequestQueue,而關於Volley的一切就是從這個時候開始的,我們就深入地學習一下在這個方法后面到底有着什么樣的實現吧。

 

我們來看看Volley類的實現:

  

  1. public class Volley {  
  2.   
  3.     ...  
  4.     public static RequestQueue newRequestQueue(Context context, HttpStack stack) {  
  5.         ...  
  6.     }  
  7.   
  8.     /** 
  9.      * Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it. 
  10.      * 
  11.      * @param context A {@link Context} to use for creating the cache dir. 
  12.      * @return A started {@link RequestQueue} instance. 
  13.      */  
  14.     public static RequestQueue newRequestQueue(Context context) {  
  15.         return newRequestQueue(context, null);  
  16.     }  
  17. }  


Volley類只有兩個方法,而主要的創建RequestQueue的方法就是包含兩個參數Context和HttpStack的newRequestQueue方法了,另外一個只是調用這個方法,將傳一個null的HttpStack而已。

 

我們看看這個方法里面的實現:

  

  1. public static RequestQueue newRequestQueue(Context context, HttpStack stack) {  
  2.     File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);//緩存文件  
  3.   
  4.     String userAgent = "volley/0";//UserAgent用來封裝應用的包名跟版本號,提供給服務器,就跟瀏覽器信息一樣  
  5.     try {  
  6.         String packageName = context.getPackageName();  
  7.         PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);  
  8.         userAgent = packageName + "/" + info.versionCode;  
  9.     } catch (NameNotFoundException e) {  
  10.     }  
  11.   
  12.     if (stack == null) {//一般我們都不需要傳這個參數進來,而volley則在這里會根據SDK的版本號來判斷   
  13.         if (Build.VERSION.SDK_INT >= 9) {  
  14.             stack = new HurlStack();//SDK如果大於等於9,也就是Android 2.3以后,因為引進了HttpUrlConnection,所以會用一個HurlStack  
  15.         } else {//如果小於9,則是用HttpClient來實現  
  16.             // Prior to Gingerbread, HttpUrlConnection was unreliable.  
  17.             // See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html  
  18.             stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));  
  19.         }  
  20.     }  
  21.   
  22.     Network network = new BasicNetwork(stack);//創建一個Network,構造函數需要一個stack參數,Network里面會調用stack去跟網絡通信  
  23. n style="white-space:pre">  </span>//創建RequestQueue,並將緩存實現DiskBasedCache和網絡實現BasicNetwork傳進去,然后調用start方法  
  24.     RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);  
  25.     queue.start();  
  26.      
  27.     return queue;  
  28. }  

大家可以看代碼中的注釋,這里簡要說明一下步驟:

 

1)創建緩存文件和UserAgenp字符串

2)根據SDK版本來創建HttpStack的實現,如果是2.3以上的,則使用基於HttpUrlConnection實現的HurlStack,反之,則利用HttpClient實現的HttpClientStack。

3)創建一個BasicNetwork對象,並將HttpStack封裝在Network中

4)創建一個DiskBasedCache對象,和Network一起,傳給RequestQueue作為參數,創建RequestQueue對象。

5)調用 RequestQueue的 start 方法,然后返回創建的queue對象。

接下來,我們看看RequestQueue的構造函數:

  

  1. public RequestQueue(Cache cache, Network network) {  
  2.     this(cache, network, DEFAULT_NETWORK_THREAD_POOL_SIZE);//跟網絡交互的線程數量,默認是4  
  3. }  

 

很明顯,調用了另外一個構造函數: 

  1. public RequestQueue(Cache cache, Network network, int threadPoolSize) {  
  2.       this(cache, network, threadPoolSize,  
  3.               new ExecutorDelivery(new Handler(Looper.getMainLooper())));  
  4.   }  


而最終會調用到下面這個構造函數來創建對象: 

  1. public RequestQueue(Cache cache, Network network, int threadPoolSize,  
  2.         ResponseDelivery delivery) {  
  3.     mCache = cache;//緩存  
  4.     mNetwork = network;//網絡  
  5.     mDispatchers = new NetworkDispatcher[threadPoolSize];//線程池  
  6.     mDelivery = delivery;//派送Response的實現  
  7. }  

在構造函數中,我們可以看到在Volley類中創建的Cache和Network。

 

另外,通過前面傳進來的線程數量(默認是4),會創建一個NetworkDispatcher的數組,也就是創建了一個有4個線程的線程池,因為NetworkDispatcher是繼承於Thread的實現類,其定義如下:

  

  1. public class NetworkDispatcher extends Thread {  

而delivery的實現則是ExecutorDelivery,我們可以看到它的參數是一個Handler,而Handler的構造函數參數則是Looper.getMainLooper(),這其實是應用的主線程的Looper,也就是說,Handler其實是主線程中的Hanlder,ExecutorDelivery的定義如下:

  

  1. public ExecutorDelivery(final Handler handler) {  
  2.     // Make an Executor that just wraps the handler.  
  3.     mResponsePoster = new Executor() {  
  4.         @Override  
  5.         public void execute(Runnable command) {  
  6.             handler.post(command);  
  7.         }  
  8.     };  
  9. }  

主要作用也就是利用Handler來將Response傳回主線程進行UI更新,比如之前的更新ImageView,因為我們知道,UI的更新必須在主線程。

 

到這里,我們的RequestQueue對象就創建好了,下面就是要調用它的start方法了。

  

  1. public void start() {  
  2.     stop();  // 保證所有正在運行的Dispatcher(也就是線程)都停止  
  3.     // 創建緩存的派發器(也是一個線程),並啟動線程。  
  4.     mCacheDispatcher = new CacheDispatcher(mCacheQueue, mNetworkQueue, mCache, mDelivery);  
  5.     mCacheDispatcher.start();  
  6.   
  7.     // 根據線程池的大小,創建相對應的NetworkDispatcher(線程),並啟動所有的線程。  
  8.     for (int i = 0; i < mDispatchers.length; i++) {  
  9.         NetworkDispatcher networkDispatcher = new NetworkDispatcher(mNetworkQueue, mNetwork,  
  10.                 mCache, mDelivery);  
  11.         mDispatchers[i] = networkDispatcher;  
  12.         networkDispatcher.start();  
  13.     }  
  14. }  
  15.   
  16. /** 
  17.  * 停止緩存線程跟所有的網絡線程 
  18.  */  
  19. public void stop() {  
  20.     if (mCacheDispatcher != null) {  
  21.         mCacheDispatcher.quit();  
  22.     }  
  23.     for (int i = 0; i < mDispatchers.length; i++) {  
  24.         if (mDispatchers[i] != null) {  
  25.             mDispatchers[i].quit();  
  26.         }  
  27.     }  
  28. }  


我們可以看到,

 

1)start方法的一開始,會先調用stop方法。stop會將緩存線程還有所有的網絡線程停止。

2)重新創建一個緩存線程,並啟動,在這里,會將 mCacheQueue,mNetwrok, mCache 和 mDelivery 傳給其構造函數。

3)根據線程池的大小,創建相對應數目的網絡線程,而在這里,我們可以看到會將 mNetworkQueue,mNetwrok,mCache 和 mDelivery作為參數傳給NetworkDispatcher。

很明顯,當調用RequestQueue的 start方法的時候,其實也就是啟動了一個緩存線程和默認的4個網絡線程,它們就會在后面靜靜地等待請求的到來。

在兩個構造函數上面,mNetwork, mCache 和 mDelivery,我們上面都介紹過了,但是 mCacheQueue 和 mNetworkQueue,這兩個具體是什么樣的呢?

 

  1. private final PriorityBlockingQueue<Request<?>> mCacheQueue =  
  2.     new PriorityBlockingQueue<Request<?>>();  
  3.   
  4. /** The queue of requests that are actually going out to the network. */  
  5. private final PriorityBlockingQueue<Request<?>> mNetworkQueue =  
  6.     new PriorityBlockingQueue<Request<?>>();  


我們可以看到它們其實都是Java並發(Concurrent)包中提供的利用優先級來執行的阻塞隊列PriorityBlockingQueue。顯然,它們就應該是來放置從外面傳進來的請求的,比如JsonRequest,ImageRequest和 StringRequest。

 

而RequestQueue類中,還有另外兩個請求集合:

 

  1. //等待中的請求集合  
  2. private final Map<String, Queue<Request<?>>> mWaitingRequests =  
  3.         new HashMap<String, Queue<Request<?>>>();  
  4.   
  5. //所有在隊列中,或者正在被處理的請求都會在這個集合中  
  6. private final Set<Request<?>> mCurrentRequests = new HashSet<Request<?>>();  

 

我們記得,當我們創建好RequestQueue對象之后,如果我們想要去加載圖片,我們就會創建ImageRequest對象,如果我們想要去獲取Json數據,我們就會創建JsonRequest對象,而最后我們都會調用 RequestQueue的add方法,來將請求加入到隊列中的。 

  1. public <T> Request<T> add(Request<T> request) {  
  2.     // 將請求的隊列設置為當前隊列,並將請求添加到mCurrentRequests中,表明是正在處理中的,而在這里,我們可以看到利用synchronized來同步  
  3.     request.setRequestQueue(this);  
  4.     synchronized (mCurrentRequests) {  
  5.         mCurrentRequests.add(request);  
  6.     }  
  7.   
  8.     // 在這里會設置序列號,保證每個請求都是按順序被處理的。  
  9.     request.setSequence(getSequenceNumber());  
  10.     request.addMarker("add-to-queue");  
  11.   
  12.     // 如果這個請求是設置不緩存的,那么就會將其添加到mNetworkQueue中,直接去網絡中獲取數據  
  13.     if (!request.shouldCache()) {  
  14.         mNetworkQueue.add(request);  
  15.         return request;  
  16.     }  
  17.   
  18.     // 到這里,表明這個請求可以去先去緩存中獲取數據。  
  19.     synchronized (mWaitingRequests) {  
  20.         String cacheKey = request.getCacheKey();  
  21.         if (mWaitingRequests.containsKey(cacheKey)) {//<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">如果這個請求已經有一個相同的請求(相同的CacheKey)在mWatingRequest中,那么就要將相同CacheKey的請求用一個LinkedList給裝起來,先不需要處理,等那個正在處理的請求結束后,再看看應該怎么處理。</span>  
  22.   
  23.             // There is already a request in flight. Queue up.  
  24.             Queue<Request<?>> stagedRequests = mWaitingRequests.get(cacheKey);  
  25.             if (stagedRequests == null) {  
  26.                 stagedRequests = new LinkedList<Request<?>>();  
  27.             }  
  28.             stagedRequests.add(request);  
  29.             mWaitingRequests.put(cacheKey, stagedRequests);  
  30.             if (VolleyLog.DEBUG) {  
  31.                 VolleyLog.v("Request for cacheKey=%s is in flight, putting on hold.", cacheKey);  
  32.             }  
  33.         } else {  
  34. n style="white-space:pre">      </span>//如果mWaitingRequest中沒有,那么就將其添加到集合中,將添加到mCacheQueue隊列中,表明現在這個cacheKey的請求已經在處理了。  
  35.             mWaitingRequests.put(cacheKey, null);  
  36.             mCacheQueue.add(request);  
  37.         }  
  38.         return request;  
  39.     }  
  40. }  

而當mCacheQueue或者mNetworkQueue利用add方法添加請求之后,在運行的線程就會接收到請求,從而去處理相對應的請求,最后將處理的結果由mDelivery來發送到主線程進行更新。

 

到這里,我們的請求就會在緩存線程或者網絡線程中去處理了,當它們結束之后,每一個Request就會調用自身的finish方法,如下:

 

  1. void finish(final String tag) {  
  2.     if (mRequestQueue != null) {  
  3.         mRequestQueue.finish(this);  
  4.     }  

而在這里,它調用的其實是 RequestQueue的finish方法,如下:

  

  1. void finish(Request<?> request) {  
  2.     // Remove from the set of requests currently being processed.  
  3.     synchronized (mCurrentRequests) {  
  4.         mCurrentRequests.remove(request);  
  5.     }  
  6.   
  7.     if (request.shouldCache()) {  
  8.         synchronized (mWaitingRequests) {  
  9.             String cacheKey = request.getCacheKey();  
  10.             Queue<Request<?>> waitingRequests = mWaitingRequests.remove(cacheKey);  
  11.             if (waitingRequests != null) {  
  12.                 if (VolleyLog.DEBUG) {  
  13.                     VolleyLog.v("Releasing %d waiting requests for cacheKey=%s.",  
  14.                             waitingRequests.size(), cacheKey);  
  15.                 }  
  16.                 // Process all queued up requests. They won't be considered as in flight, but  
  17.                 // that's not a problem as the cache has been primed by 'request'.  
  18.                 mCacheQueue.addAll(waitingRequests);  
  19.             }  
  20.         }  
  21.     }  
  22. }  


可以看到,第一步就是將請求從mCurrentRequests中移除,正好對應了上面add方法中的添加。

 

第二步就是判斷這個請求有沒有緩存,如果有,那么我們這個時候,將前面mWaitingQueue中相同CacheKey的一大批請求再一股腦兒的扔到mCacheQueue中,為什么現在才扔呢?因為前面我們不知道相同CacheKey的那個請求到底在緩存中有沒有,如果沒有,它需要去網絡中獲取,那就等到它從網絡中獲取之后,放到緩存中后,它結束了,並且已經緩存了,這個時候,我們就可以保證后面那堆相同CacheKey的請求可以在緩存中去取到數據了,而不需要再去網絡中獲取了。

在RequestQueue中,還提供了兩個方法去取消請求,如下:

  

  1. public void cancelAll(RequestFilter filter) {  
  2.     synchronized (mCurrentRequests) {  
  3.         for (Request<?> request : mCurrentRequests) {  
  4.             if (filter.apply(request)) {  
  5.                 request.cancel();  
  6.             }  
  7.         }  
  8.     }  
  9. }  
  10.   
  11. /** 
  12.  * Cancels all requests in this queue with the given tag. Tag must be non-null 
  13.  * and equality is by identity. 
  14.  */  
  15. public void cancelAll(final Object tag) {  
  16.     if (tag == null) {  
  17.         throw new IllegalArgumentException("Cannot cancelAll with a null tag");  
  18.     }  
  19.     cancelAll(new RequestFilter() {  
  20.         @Override  
  21.         public boolean apply(Request<?> request) {  
  22.             return request.getTag() == tag;  
  23.         }  
  24.     });  
  25. }  


如上,第一個cancleAll會獲取一個RequestFilter,這是RequestQueue的內部接口,定義如下:

  

  1. public interface RequestFilter {  
  2.     public boolean apply(Request<?> request);  
  3. }  

我們需要自己去實現,什么樣的請求才是符合我們的過濾器的,然后在cancel中根據我們定義的過濾規則去批量地取消請求。

 

而第二個則是利用創建Request時設置的Tag值,實現RequestFilter,然后調用上一個cancelAll方法,來取消一批同個Tag值的請求。

這兩個方法(其實是一種,主要是利用Tag來批量取消請求)跟我們這個流程的關系不大,所以就不在這里多講了。

嗯,關於RequestQueue中一切,到這里,也就結束了,不知道講得清不清楚,還希望大家多給點建議。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM