@Override public HttpResponse executeRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError { String url = request.getUrl(); // Log.i("jinwei"," ## executeRequest = "+url); HashMap<String, String> 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. 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."); }
if (!hasResponseBody(request.getMethod(), responseCode)) { return new HttpResponse(responseCode, convertHeaders(connection.getHeaderFields())); }
return new HttpResponse(responseCode, convertHeaders(connection.getHeaderFields()), connection.getContentLength(), inputStreamFromConnection(connection)); }
public void start() { stop(); // Make sure any currently running dispatchers are stopped. // Create the cache dispatcher and start it. mCacheDispatcher = new CacheDispatcher(mCacheQueue, mNetworkQueue, mCache, mDelivery); mCacheDispatcher.start();
// Create network dispatchers (and corresponding threads) up to the pool size. Log.i("jinwei"," ## mDispatchers.length ## " +mDispatchers.length); for (int i = 0; i < mDispatchers.length; i++) { NetworkDispatcher networkDispatcher = new NetworkDispatcher(mNetworkQueue, mNetwork, mCache, mDelivery); mDispatchers[i] = networkDispatcher; networkDispatcher.start(); } }
这里总共开启了5个 Thread,一个CacheThread和四个NetWorkThread。
NetWorkThread的run方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
@Override public void run() { Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); while (true) { try { Log.i("jinwei"," ## run ##"); processRequest(); } catch (InterruptedException e) { // We may have been interrupted because it was time to quit. Log.i("jinwei"," ## mQuit ##"); if (mQuit) { return; } } } }
private void processRequest() throws InterruptedException { long startTimeMs = SystemClock.elapsedRealtime(); // Take a request from the queue. Request<?> request = mQueue.take();
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(); return; }
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"); request.notifyListenerResponseNotUsable(); return; }
// 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"); }