假设各位看官细致看过我之前的文章,实际上Network这块的仅仅是点小功能的补充。我们来看下NetworkDispatcher的核心处理逻辑:

<span style="font-size:18px;">while (true) {
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");
continue;
} addTrafficStatsTag(request); // Perform the network request.
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");
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);
} catch (VolleyError volleyError) {
parseAndDeliverNetworkError(request, volleyError);
} catch (Exception e) {
VolleyLog.e(e, "Unhandled exception %s", e.toString());
mDelivery.postError(request, new VolleyError(e));
}
}</span>

我们看到NetworkDispatcher和CacheDispatcher的差别在于

<span style="font-size:18px;">                addTrafficStatsTag(request);

                // Perform the network request.
NetworkResponse networkResponse = mNetwork.performRequest(request);
request.addMarker("network-http-complete");</span>

addTrafficStatsTag(request);方法用来优化网络通道,并不属于我们聊的重点。我们知道mNetwork通过HttpStack来实现网络请求,我们先来看一下HurlStack。

<span style="font-size:18px;">@Override
public HttpResponse performRequest(Request<? > request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError {
String url = request.getUrl();
HashMap<String, String> map = new HashMap<String, String>();
map.putAll(request.getHeaders());
map.putAll(additionalHeaders);
if (mUrlRewriter != null) {
String rewritten = mUrlRewriter.rewriteUrl(url);
if (rewritten == null) {
throw new IOException("URL blocked by rewriter: " + url);
}
url = rewritten;
}
URL parsedUrl = new URL(url);
HttpURLConnection connection = openConnection(parsedUrl, request);
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, map.get(headerName));
}
setConnectionParametersForRequest(connection, request);
// Initialize HttpResponse with data from the HttpURLConnection.
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
int responseCode = connection.getResponseCode();
if (responseCode == -1) {
// -1 is returned by getResponseCode() if the response code could not be retrieved.
// Signal to the caller that something was wrong with the connection.
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
StatusLine responseStatus = new BasicStatusLine(protocolVersion,
connection.getResponseCode(), connection.getResponseMessage());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
response.setEntity(entityFromConnection(connection));
for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
if (header.getKey() != null) {
Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
response.addHeader(h);
}
}
return response;
}</span>

HurlStack是通过HttpUrlConnection的方式来请求数据。同一时候,它也听过了接口 UrlRewriter mUrlRewriter;来实现url的重定向。HurlStack返回了HttpResponse给上层NetworkDispatcher.HttpClientStack大同小异我们就不看了。当然我们也看出了CacheDispatcher和NetworkDispatcher代码上的不足,可是都是些小瑕疵。由于对于Request.isCancel这些方面的推断全然能够採用模板方法的方式来统一。

NetworkDispatcher通过Network取得HttpResponse之后依然跟CacheDispacher一样用Request进行包装:

<span style="font-size:18px;"><span style="font-size:18px;">Response<?> response = request.parseNetworkResponse(networkResponse);
request.addMarker("network-parse-complete");</span></span>

最后推断Request是否须要Cache假设须要就放到Cache缓冲池里面:

<span style="font-size:18px;">if (request.shouldCache() && response.cacheEntry != null) {
mCache.put(request.getCacheKey(), response.cacheEntry);
request.addMarker("network-cache-written");
}</span>

那么NetworkDispatcher的基本流程就走完了。下一章,我们将针对Volley提供给我们方便api结合源代码来看待Volley.

最新文章

  1. 踩石行动:ViewPager无限轮播的坑
  2. 动态SQL语句之sp_executesql的使用
  3. Navigator
  4. docker -v挂载数据卷网络异常的问题
  5. Redis集群创建报错
  6. 开博第一篇:DHT 爬虫的学习记录
  7. 在ASP.NET中,IE与Firefox下载文件带汉字名时乱码的解决方法
  8. 关于git的文件内容冲突解决
  9. 【Xilinx-Petalinux学习】-00-开始
  10. 增强for循环用法
  11. Mysql配置文件my.cnf详细说明
  12. Burp_用户名密码爆破
  13. [LeetCode] Shortest Completing Word 最短完整的单词
  14. nginx+uwsgi+django 部署原理
  15. golang:使用timingwheel进行大量ticker的优化
  16. 第六十五天 js操作
  17. body-parser 用法
  18. 【linux】进程状态
  19. jQuery中使用attribute,prop获取,设置input的checked值【转】
  20. InvalidateRect,invalidate,updatewindow(转)

热门文章

  1. [转] Moran&#39;s I
  2. jvm分析内存泄露
  3. SecureCRT connecting VM Linux show error message: The remote system refused the connection.
  4. Unity3D 手游开发中所有特殊的文件夹
  5. nginx bind() to 0.0.0.0:**** failed (13: Permission denied)
  6. xampp Apache Access forbidden! Error 403解决方法
  7. scrapy处理需要跟进的url
  8. matplotlib绘制常用统计图
  9. 【转】go语言的字节序
  10. TCP的状态(SYN,FIN等)