先看看google給的注釋:
/**
* Provides a thread for performing network dispatch from a queue of requests.
*
* Requests added to the specified queue are processed from the network via a
* specified {@link Network} interface. Responses are committed to cache, if
* eligible, using a specified {@link Cache} interface. Valid responses and
* errors are posted back to the caller via a {@link ResponseDelivery}.
*/
在RequestQueue 中構造了此類的對象并且調用了其start方法,下來我們來看看它是怎么執(zhí)行的。因為繼承了Thread,來看一看其run方法 。
@Override
public void run() {
//將線程設置為最高級
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
while (true) { //如果調用RequestQueue的add方法,這里是while(true)的,將會在這馬上收到請求。
//獲取系統(tǒng)時間
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 {
//添加狀態(tài)標記
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.//請求網絡 用mNetwork請求網絡得到NetworkResponse
NetworkResponse networkResponse = mNetwork.performRequest(request);
//添加狀態(tài)標記
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.
//304的狀態(tài) 以及處理
if (networkResponse.notModified && request.hasHadResponseDelivered()) {
request.finish("not-modified");
request.notifyListenerResponseNotUsable();
continue;
}
// Parse the response here on the worker thread. 在工作線程對響應進行parse
Response<?> response = request.parseNetworkResponse(networkResponse);
//添加狀態(tài)標記
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 將request和response 發(fā)送回主線程
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();
}
}
}
private void parseAndDeliverNetworkError(Request<?> request, VolleyError error) {
error = request.parseNetworkError(error);
mDelivery.postError(request, error);
}
這就是NetworkDispatcher 分發(fā)的過程,接下來將分析BasicNetwork 類。