編輯:關於Android編程
如果各位看官仔細看過我之前的文章,實際上Network這塊的只是點小功能的補充。我們來看下NetworkDispatcher的核心處理邏輯:
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)); } }我們看到NetworkDispatcher和CacheDispatcher的區別在於
addTrafficStatsTag(request); // Perform the network request. NetworkResponse networkResponse = mNetwork.performRequest(request); request.addMarker("network-http-complete");addTrafficStatsTag(request);方法用來優化網絡通道,並不屬於我們聊的重點。我們知道mNetwork通過HttpStack來實現網絡請求,我們先來看一下HurlStack。
@Override public HttpResponse performRequest(Request> request, MapadditionalHeaders) throws IOException, AuthFailureError { String url = request.getUrl(); HashMap map = new HashMap (); 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 > header : connection.getHeaderFields().entrySet()) { if (header.getKey() != null) { Header h = new BasicHeader(header.getKey(), header.getValue().get(0)); response.addHeader(h); } } return response; }
NetworkDispatcher通過Network取得HttpResponse之後依舊跟CacheDispacher一樣用Request進行包裝:
Response> response = request.parseNetworkResponse(networkResponse); request.addMarker("network-parse-complete");最後判斷Request是否需要Cache如果需要就放到Cache緩沖池裡面:
if (request.shouldCache() && response.cacheEntry != null) { mCache.put(request.getCacheKey(), response.cacheEntry); request.addMarker("network-cache-written"); }那麼NetworkDispatcher的基本流程就走完了。下一章,我們將針對Volley提供給我們方便api結合源碼來看待Volley.
本文實例為大家分享了Android下拉刷新的具體代碼,供大家參考,具體內容如下MainActivity.java代碼:package siso.refreshablev;
一、適配器 ListItemClickAdapterpublic class ListItemClickAdapter extends BaseAdapter { pr
一、概述之前寫了篇Android OkHttp完全解析 是時候來了解OkHttp了,其實主要是作為okhttp的普及文章,當然裡面也簡單封裝了工具類,沒想到
Android的窗口體系中,WindowManager占有非常重要的地位,它封裝了添加、移除、更新窗口的方法,它是Activity、View的更加底層的管理類,使用Win
compile 'com.android.suppo