2017-10-9(Volley使用範例原始碼分析)

wyman_1007發表於2017-12-13

Volley應該是比較久遠的產物了。google在2013 IO釋出,但也可以借鑑學習畢竟是google工程師的AOSP產物。
下面從範例程式碼分析Volley的結構和核心原始碼。

//建立RequestQueue 佇列
RequestQueue mQueue = Volley.newRequestQueue(context);
//url
String url = "http//www.baidu.com";
//返回結果處理
Response.Listener listener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("TAG",response);
}
};
//錯誤返回結果處理
Response.ErrorListener errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("TAG",error.getMessage(),error);
}
};
//Request請求
StringRequest stringRequest = new StringRequest(Request.Method.GET,url,
listener, errorListener);
//將請求加入RequestQueue 佇列
mQueue.add(stringRequest);
複製程式碼

實際就三個步驟:

  • 建立佇列 RequestQueue
  • 建立Request請求
  • 將Request請求新增到佇列RequestQueue

建立RequestQueue

RequestQueue mQueue = Volley.newRequestQueue(context);

Paste_Image.png
呼叫兩個引數的構造方法:

Paste_Image.png

Paste_Image.png
這個方法有點長擷取重要部分
主要是對於9以上版本是建立BasicNetwork物件
然後呼叫newRequestQueue(context, network);
看看BasicNetwork物件幹什麼的?

Paste_Image.png
建立了一個4*1024大小的二進位制陣列和將new HurlStack()傳進來。
好,看到這裡先把這個類放著,後邊再繼續看到底做了些什麼。

將context和BasicNetwork傳進來

newRequestQueue(context, network);

Paste_Image.png
第一句程式碼就不說了,就是在裝置上建立一個檔案放快取;

主要看後面兩句:

RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
queue.start();

Paste_Image.png

Paste_Image.png
這裡看見建立了一個ExecutorDelivery,和我們十分熟悉的Handler,而這個Handler是主執行緒的Handler;看看這個ExecutorDelivery是幹什麼的:

Paste_Image.png

看到這裡,ExecutorDelivery主要就是建立一個主執行緒的Handler,將返回的資訊給主執行緒的Handler處理。Runnable 就是給主執行緒處理的。
繼續看剛剛的構造方法

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

Paste_Image.png

  • cache 就是DiskBasedCache主要就是快取
  • network 就是BasicNetwork主要就是傳送http請求的,但真正實現傳送是HurlStack,後面會帶大家看原始碼講解。
  • mDispatchers就是建立一個NetworkDispatcher陣列
  • mDelivery 剛才已經看了程式碼就是給主執行緒Handler處理回撥結果用的

RequestQueue建立完畢下一步

queue.start()

Paste_Image.png
stop();英文註解寫得很清楚就是確保目前正在執行的dispatcher停止(不管是網路的還是快取的)

Paste_Image.png

之後的程式碼逐行解釋

mCacheDispatcher = new CacheDispatcher(mCacheQueue, mNetworkQueue, mCache, mDelivery);

  • mCacheQueue是快取佇列
  • mNetworkQueue是網路請求佇列
  • mCache剛才說了是DiskBasedCache主要就是快取
  • mDelivery剛才已經看了程式碼就是給主執行緒Handler處理回撥結果用的

Paste_Image.png
這裡多了個WaitingRequestManager,看看幹什麼的:

Paste_Image.png
先放著,建構函式並沒有幹什麼

mCacheDispatcher.start();
我們看看CacheDispatcher類

Paste_Image.png
繼承Tread,那麼start()即是呼叫run()

@Override
public void run() {
if (DEBUG) VolleyLog.v("start new dispatcher");
//最高優先順序
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
// Make a blocking call to initialize the cache.
mCache.initialize();
while (true) {
try {
// Get a request from the cache triage queue, blocking until
// at least one is available.
final Request<?> request = mCacheQueue.take();
request.addMarker("cache-queue-take");
// If the request has been canceled, don't bother dispatching it.
if (request.isCanceled()) {
request.finish("cache-discard-canceled");
continue;
}
// Attempt to retrieve this item from cache.
Cache.Entry entry = mCache.get(request.getCacheKey());
if (entry == null) {
request.addMarker("cache-miss");
// Cache miss; send off to the network dispatcher.
if (!mWaitingRequestManager.maybeAddToWaitingRequests(request)) {
mNetworkQueue.put(request);
}
continue;
}
// If it is completely expired, just send it to the network.
if (entry.isExpired()) {
request.addMarker("cache-hit-expired");
request.setCacheEntry(entry);
if (!mWaitingRequestManager.maybeAddToWaitingRequests(request)) {
mNetworkQueue.put(request);
}
continue;
}
// We have a cache hit; parse its data for delivery back to the request.
request.addMarker("cache-hit");
Response<?> response = request.parseNetworkResponse(
new NetworkResponse(entry.data, entry.responseHeaders));
request.addMarker("cache-hit-parsed");
if (!entry.refreshNeeded()) {
// Completely unexpired cache hit. Just deliver the response.
//傳送請求
mDelivery.postResponse(request, response);
} else {
// Soft-expired cache hit. We can deliver the cached response,
// but we need to also send the request to the network for
// refreshing.
request.addMarker("cache-hit-refresh-needed");
request.setCacheEntry(entry);
// Mark the response as intermediate.
response.intermediate = true;
if (!mWaitingRequestManager.maybeAddToWaitingRequests(request)) {
// Post the intermediate response back to the user and have
// the delivery then forward the request along to the network.
mDelivery.postResponse(request, response, new Runnable() {
@Override
public void run() {
try {
mNetworkQueue.put(request);
} catch (InterruptedException e) {
// Restore the interrupted status
Thread.currentThread().interrupt();
}
}
});
} else {
// request has been added to list of waiting requests
// to receive the network response from the first request once it returns.
mDelivery.postResponse(request, response);
}
}
} catch (InterruptedException e) {
// We may have been interrupted because it was time to quit.
if (mQuit) {
return;
}
}
}
}
複製程式碼

方法比較長,我說重點:
首先設定了該執行緒為最高階別,然後快取初始化,之後就是一個無限迴圈。裡面首先在快取佇列拿出一個request,如果該request是已經取消就退出迴圈。不是繼續往下走,從快取mCache取一個entry,如果是空的話,mWaitingRequestManager.maybeAddToWaitingRequests(request),呼叫這句。看看幹什麼的:

private synchronized boolean maybeAddToWaitingRequests(Request<?> request) {
String cacheKey = request.getCacheKey();
// Insert request into stage if there's already a request with the same cache key
// in flight.
if (mWaitingRequests.containsKey(cacheKey)) {
// There is already a request in flight. Queue up.
List<Request<?>> stagedRequests = mWaitingRequests.get(cacheKey);
if (stagedRequests == null) {
stagedRequests = new ArrayList<Request<?>>();
}
request.addMarker("waiting-for-response");
stagedRequests.add(request);
mWaitingRequests.put(cacheKey, stagedRequests);
if (VolleyLog.DEBUG) {
VolleyLog.d("Request for cacheKey=%s is in flight, putting on hold.", cacheKey);
}
return true;
} else {
// Insert 'null' queue for this cacheKey, indicating there is now a request in
// flight.
mWaitingRequests.put(cacheKey, null);
request.setNetworkRequestCompleteListener(this);
if (VolleyLog.DEBUG) {
VolleyLog.d("new request, sending to network %s", cacheKey);
}
return false;
}
}
複製程式碼

Paste_Image.png
由於我們是第一次作請求,基本就沒有快取,所以是走else部分:

Paste_Image.png
裡面只將cacheKey給mWaitingRequests存了起來,然後就是我截圖紅色框部分,最後返回false;
setNetworkRequestCompleteListener實現介面,其實就是這個:

Paste_Image.png
繼續看回之前的:

Paste_Image.png
因為返回false,即呼叫mNetworkQueue.put(request);
網路佇列新增了一個request,然後跳出迴圈;

繼續看回RequestQueue的start():

Paste_Image.png
剛才走到紅色箭頭,繼續往下走:
由於mDispathcers的容量設定了4個,所以迴圈4次:
裡面就是new一個NetworkDispatcher

Paste_Image.png

  • mNetworkQueue是網路佇列
  • network 就是BasicNetwork主要就是傳送http請求的,但真正實現傳送是HurlStack
  • cache 就是DiskBasedCache主要就是快取
  • mDelivery 剛才已經看了程式碼就是給主執行緒Handler處理回撥結果用的

networkDispatcher.start();
看看NetworkDispatcher類:

Paste_Image.png
和CacheDispatcher類一樣,就不多說,直接看run()

 @Override
public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
while (true) {
//獲取系統開機直到現在的時間包含睡眠時間
long startTimeMs = SystemClock.elapsedRealtime();
Request<?> request;
try {
// Take a request from the queue.
request = mQueue.take();
} catch (InterruptedException e) {
// We may have been interrupted because it was time to quit.
if (mQuit) {
return;
}
continue;
}
try {
request.addMarker("network-queue-take");
// If the request was cancelled already, do not perform the
// network request.
if (request.isCanceled()) {
request.finish("network-discard-cancelled");
request.notifyListenerResponseNotUsable();
continue;
}
addTrafficStatsTag(request);
// Perform the network request.
//執行http請求mNetwork 是BasicNetwork
NetworkResponse networkResponse = mNetwork.performRequest(request);
request.addMarker("network-http-complete");
// If the server returned 304 AND we delivered a response already,
// we're done -- don't deliver a second identical response.
if (networkResponse.notModified && request.hasHadResponseDelivered()) {
request.finish("not-modified");
request.notifyListenerResponseNotUsable();
continue;
}
// Parse the response here on the worker thread.
Response<?> response = request.parseNetworkResponse(networkResponse);
request.addMarker("network-parse-complete");
// Write to cache if applicable.
// TODO: Only update cache metadata instead of entire record for 304s.
if (request.shouldCache() && response.cacheEntry != null) {
mCache.put(request.getCacheKey(), response.cacheEntry);
request.addMarker("network-cache-written");
}
// Post the response back.
request.markDelivered();
mDelivery.postResponse(request, response);
request.notifyListenerResponseReceived(response);
} catch (VolleyError volleyError) {
volleyError.setNetworkTimeMs(SystemClock.elapsedRealtime() - startTimeMs);
parseAndDeliverNetworkError(request, volleyError);
request.notifyListenerResponseNotUsable();
} catch (Exception e) {
VolleyLog.e(e, "Unhandled exception %s", e.toString());
VolleyError volleyError = new VolleyError(e);
volleyError.setNetworkTimeMs(SystemClock.elapsedRealtime() - startTimeMs);
mDelivery.postError(request, volleyError);
request.notifyListenerResponseNotUsable();
}
}
}
複製程式碼

NetworkDispatcher這個類和CacheDispatcher很相似,不同之處只是CacheDispatcher是通過快取獲得返回結果,而NetworkDispatcher是通過傳送網路請求獲得。

Process.setThreadPriority(Process.THREADPRIORITYBACKGROUND);

第一步也是設定該執行緒為最高階別;然後就是一個無限迴圈;

request = mQueue.take();

就是從網路集合獲取request物件;

Paste_Image.png
這句程式碼與CacheDispatcher不同,看到註解啦吧;

這裡mNetwork是什麼呢?其實就是

Paste_Image.png
BasicNetwork,看看它的performRequest()方法:

/**
* 執行Request請求
* */

@Override
public NetworkResponse performRequest(Request<?> request) throws VolleyError
{
//返回一個開機以來包括睡眠的度過時間
long requestStart = SystemClock.elapsedRealtime();
while (true) {
HttpResponse httpResponse = null;
byte[] responseContents = null;
List<Header> responseHeaders = Collections.emptyList();
try {
// Gather headers.
Map<String, String> additionalRequestHeaders =
getCacheHeaders(request.getCacheEntry());
//返回http結果
httpResponse = mBaseHttpStack.executeRequest(request, additionalRequestHeaders);
//狀態碼
int statusCode = httpResponse.getStatusCode();
//response頭部
responseHeaders = httpResponse.getHeaders();
// Handle cache validation.
/**
* 當出現304(服務端有快取且有效)會自己構建一個Response返回
* */

if (statusCode == HttpURLConnection.HTTP_NOT_MODIFIED) {
/**
* 304:
* Not Modified 客戶端有緩衝的文件併發出了一個條件性的請求(一般是提供If-Modified-Since頭表示客戶只想比指定日期更新的文件
* 返回 304 的時候已經做了一次資料庫查詢,但是可以避免接下來更多的資料庫查詢,
* 並且沒有返回頁面內容而只是一個 HTTP Header,
* 從而大大的降低頻寬的消耗,對於使用者的感覺也是提高。
* 伺服器會自動完成 Last Modified(快取檔案的) 和 If Modified Since(請求中包含的)
* */

Entry entry = request.getCacheEntry();
if (entry == null) {
return new NetworkResponse(HttpURLConnection.HTTP_NOT_MODIFIED, null, true,
SystemClock.elapsedRealtime() - requestStart, responseHeaders);
}
// Combine cached and response headers so the response will be complete.
//entry為Response的body
List<Header> combinedHeaders = combineHeaders(responseHeaders, entry);
return new NetworkResponse(HttpURLConnection.HTTP_NOT_MODIFIED, entry.data,
true, SystemClock.elapsedRealtime() - requestStart, combinedHeaders);
}
// Some responses such as 204s do not have content. We must check.
InputStream inputStream = httpResponse.getContent();
if (inputStream != null) {
responseContents =
inputStreamToBytes(inputStream, httpResponse.getContentLength());
} else {
// Add 0 byte response as a way of honestly representing a
// no-content request.
responseContents = new byte[0];
}
// if the request is slow, log it.
long requestLifetime = SystemClock.elapsedRealtime() - requestStart;
//記錄請求時間
logSlowRequests(requestLifetime, request, responseContents, statusCode);
if (statusCode < 200 || statusCode > 299) {
throw new IOException();
}
return new NetworkResponse(statusCode, responseContents, false,
SystemClock.elapsedRealtime() - requestStart, responseHeaders);
} catch (SocketTimeoutException e) {
attemptRetryOnException("socket", request, new TimeoutError());
} catch (MalformedURLException e) {
throw new RuntimeException("Bad URL " + request.getUrl(), e);
} catch (IOException e) {
int statusCode;
if (httpResponse != null) {
statusCode = httpResponse.getStatusCode();
} else {
throw new NoConnectionError(e);
}
VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());
NetworkResponse networkResponse;
if (responseContents != null) {
networkResponse = new NetworkResponse(statusCode, responseContents, false,
SystemClock.elapsedRealtime() - requestStart, responseHeaders);
if (statusCode == HttpURLConnection.HTTP_UNAUTHORIZED ||
statusCode == HttpURLConnection.HTTP_FORBIDDEN) {
attemptRetryOnException("auth",
request, new AuthFailureError(networkResponse));
} else if (statusCode >= 400 && statusCode <= 499) {
// Don't retry other client errors.
throw new ClientError(networkResponse);
} else if (statusCode >= 500 && statusCode <= 599) {
if (request.shouldRetryServerErrors()) {
attemptRetryOnException("server",
request, new ServerError(networkResponse));
} else {
throw new ServerError(networkResponse);
}
} else {
// 3xx? No reason to retry.
throw new ServerError(networkResponse);
}
} else {
attemptRetryOnException("network", request, new NetworkError());
}
}
}
}
複製程式碼

很長一大段,我們看核心的:

Paste_Image.png
一開始想通過request獲取快取entry通過entry獲取http頭部資訊;
然後

httpResponse = mBaseHttpStack.executeRequest(request, additionalRequestHeaders);

mBaseHttpStack又是什麼玩意呢?
其實就是之前

Paste_Image.png
所以上邊我已經透露過真正執行http請求的是HurlStack

這裡我不過多截圖了,有興趣可以看看Volley原始碼HurlStack的executeRequest方法。其實這裡它是通過HttpURLConnection來完成http請求的。相信大家對HttpURLConnection都很熟悉,在沒有使用過任何網路請求框架的時候Android不是使用Apache的httpclient就是使用java裡面的HttpURLConnection。

我們繼續BasicNetwork的performRequest:

Paste_Image.png

請求返回獲取狀態碼,獲取返回的頭部資訊
通過狀態碼判斷如果是304的話:
說明客戶端有緩衝的文件,文件資訊還是有效的。

如果不是304狀態碼,

Paste_Image.png

獲取了返回的內容
最後返回NetworkResponse物件

Paste_Image.png

繼續看回NetworkDispatcher:
獲取到返回的NetworkResponse

Paste_Image.png

request.parseNetworkResponse(networkResponse);
這句程式碼只是將返回的內容通過頭部編碼資訊轉換一下;

其實這裡還有一段程式碼就是將返回的資訊作快取,下次如果有同一個請求的時候如果狀態碼返回304就可以通過快取獲取不走網路請求內容了。

核心程式碼是

mDelivery.postResponse(request, response);
這段程式碼其實就是ExcutorDelivery的postResponse

Paste_Image.png

下面截圖是ResponseDeliveryRunnable的run方法

Paste_Image.png
就是將返回結果給Request的deliverResponse或deliverError

Paste_Image.png
這裡只截了deliverResponse,看看裡面其實就是我們範例程式碼的listener。

整個Volley的請求流程就是這樣一個流程了。現在總結一下:

  • Volley實際是通過HttpURLConnection完成網路請求的;
  • Volley最大的特點是有快取佇列,先走快取佇列,快取佇列沒有資訊再走網路佇列;
  • Volley沒有建立執行緒池,而是預設建立4個Thread執行網路請求;
  • 最後Volley使用了PriorityBlockingQueue這個佇列,該佇列特點執行緒安全的可以根據優先順序別獲取元素,而存放在PriorityBlockingQueue裡面的元素需要實現Comparable介面;

Paste_Image.png

Paste_Image.png

最後放一張自己畫的VolleyUML圖,巨集觀理解一下Volley的結構:

VolleyUML.png

由於只是從範例程式碼的角度看原始碼,有些類沒有涉及到看,如果有什麼問題歡迎指正和學習,共同交流。

相關文章