如果各位看官仔细看过我之前的文章,实际上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.