android网络开源框架volley(三)——请求队列是主线

忽忽,很意外,第二篇大家反映还不错哈,谢谢咯~~对于这篇,要写好信心不大,不过我会尽量的。


1、从哪里开始:

从哪里开始,菜鸟能想到的当然是从最先接触的地方开始啦~~ 

首先我们看volley.java这个类:

    /**
     * Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
     *
     * @param context A {@link Context} to use for creating the cache dir.
     * @param stack An {@link HttpStack} to use for the network, or null for default.
     * @return A started {@link RequestQueue} instance.
     */
    public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
        File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);

        String userAgent = "volley/0";
        try {
            String packageName = context.getPackageName();
            PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
            userAgent = packageName + "/" + info.versionCode;
        } catch (NameNotFoundException e) {
        }

        if (stack == null) {
            if (Build.VERSION.SDK_INT >= 9) {
                stack = new HurlStack();
            } else {
                // Prior to Gingerbread, HttpUrlConnection was unreliable.
                // See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
                stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
            }
        }

        Network network = new BasicNetwork(stack);

        RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
        queue.start();

        return queue;
    }
回忆下之前我们就是通过它拿到我们的请求队列,在对newRequestQueue的描述中我们也得到验证:创建一个工作池(带优先级的阻塞队列)的实例。在这里有两个静态的构造方法,除去我们之前使用过的,另一个多了一个参数HttpStack(HttpStack稍后分析)。而我们之前并没有使用这个带HttpStack的构造方法。因此就有了上面对stack==null的判断:根据不同的版本创建不同的实例。判断的原因注释中也写的很明确: Gingerbread(2.3)之前,HttpUrlConnection不可用。这里就很自然的想到这样一个问题:Gingerbread(2.3)之前和之后的版本在网络请求上有何区别(或者HTTP Client与HttpURLConnection的区别)?如果那个网页你打不开,可以在这里下载英文原版,或者去看大牛翻译好的。完成HttpStack的创建之后紧接着有创建了一个Network和RequestQueue对象:Network执行网络请求;RequestQueue是A request dispatch queue with a thread pool of dispatchers。下面我们将围绕着RequestQueue进行展开。对了,还有一个HttpStack接口,只有一个方法,这里简单提一下:

public interface HttpStack {
    /**
     * Performs an HTTP request with the given parameters.
     *
     * <p>A GET request is sent if request.getPostBody() == null. A POST request is sent otherwise,
     * and the Content-Type header is set to request.getPostBodyContentType().</p>
     *
     * @param request the request to perform
     * @param additionalHeaders additional headers to be sent together with
     *         {@link Request#getHeaders()}
     * @return the HTTP response
     */
    public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws IOException, AuthFailureError;

}
使用给定的参数执行HTTP请求。通过上面的判断我们知道它有两个实现类:HurlStack和HttpClientStack。这样我们就得到下面的一个关系图:

这样他们的关系就相当明了了。


2、请求队列:RequestQueue

直接看关键构造函数:
   /**
     * Creates the worker pool. Processing will not begin until {@link #start()} is called.
     *
     * @param cache A Cache to use for persisting responses to disk
     * @param network A Network interface for performing HTTP requests
     * @param threadPoolSize Number of network dispatcher threads to create
     * @param delivery A ResponseDelivery interface for posting responses and errors
     */
    public RequestQueue(Cache cache, Network network, int threadPoolSize,
            ResponseDelivery delivery) {
        mCache = cache;
        mNetwork = network;
        mDispatchers = new NetworkDispatcher[threadPoolSize];
        mDelivery = delivery;
    }
这里我们传入了一个cache、Network、线程池大小和传递响应的对象。默认的线程池大小是4。创建这个RequestQueue对象之后,我们需要调用它的start()方法,完成对调度器的初始化工作,如根据传入线程池大小创建相应数量的Dispatcher(这里我们暂时只关注网络处理线程),然后启动:
    /**
     * Starts the dispatchers in this queue.
     */
    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.
        for (int i = 0; i < mDispatchers.length; i++) {
            NetworkDispatcher networkDispatcher = new NetworkDispatcher(mNetworkQueue, mNetwork,
                    mCache, mDelivery);
            mDispatchers[i] = networkDispatcher;
            networkDispatcher.start();
        }
    }
NetworkDispatcher的构造函数中传入了一个mNetworkQueue对象,这是一个带优先级的阻塞队列:
/** The queue of requests that are actually going out to the network. */
private final PriorityBlockingQueue<Request> mNetworkQueue = new PriorityBlockingQueue<Request>();
另一个需要关注的就是add方法:
/**
     * Adds a Request to the dispatch queue.
     * @param request The request to service
     * @return The passed-in request
     */
    public Request add(Request request) {
        // Tag the request as belonging to this queue and add it to the set of current requests.
        request.setRequestQueue(this);
        synchronized (mCurrentRequests) {
            mCurrentRequests.add(request);
        }

        // Process requests in the order they are added.
        request.setSequence(getSequenceNumber());
        request.addMarker("add-to-queue");

        // If the request is uncacheable, skip the cache queue and go straight to the network.
        if (!request.shouldCache()) {
            mNetworkQueue.add(request);
            return request;
        }

        // Insert request into stage if there‘s already a request with the same cache key in flight.
        synchronized (mWaitingRequests) {
            String cacheKey = request.getCacheKey();
            if (mWaitingRequests.containsKey(cacheKey)) {
                // There is already a request in flight. Queue up.
                Queue<Request> stagedRequests = mWaitingRequests.get(cacheKey);
                if (stagedRequests == null) {
                    stagedRequests = new LinkedList<Request>();
                }
                stagedRequests.add(request);
                mWaitingRequests.put(cacheKey, stagedRequests);
                if (VolleyLog.DEBUG) {
                    VolleyLog.v("Request for cacheKey=%s is in flight, putting on hold.", cacheKey);
                }
            } else {
                // Insert ‘null‘ queue for this cacheKey, indicating there is now a request in
                // flight.
                mWaitingRequests.put(cacheKey, null);
                mCacheQueue.add(request);
            }
            return request;
        }
    }
通过上面的代码我们可以看出,volley是将我们创建的Request对象直接加到上面的mNetworkQueue中。这样我们很自然的联想到,对request的处理应该是由start中创建的NetworkDispatcher完成的。NetworkDispatcher继承了Thread,run()方法是这样实现的:
 public void run() {
        Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
        Request request;
        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;
                }

                // Tag the request (if API >= 14)
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                    TrafficStats.setThreadStatsTag(request.getTrafficStatsTag());
                }

                // 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));
            }
        }
    }
首先从请求队列中取出一个请求;然后通过Network开始执行这个request请求,这个时候会拿到请求返回的结果;接着把请求的结果返给request进行处理:
Response<?> response = request.parseNetworkResponse(networkResponse);
request处理之后会返回一个response,这个response和request最后会传递到ResponseDeliveryRunnable中处理:
   /**
     * A Runnable used for delivering network responses to a listener on the
     * main thread.
     */
    @SuppressWarnings("rawtypes")
    private class ResponseDeliveryRunnable implements Runnable {
        private final Request mRequest;
        private final Response mResponse;
        private final Runnable mRunnable;

        public ResponseDeliveryRunnable(Request request, Response response, Runnable runnable) {
            mRequest = request;
            mResponse = response;
            mRunnable = runnable;
        }

        @SuppressWarnings("unchecked")
        @Override
        public void run() {
            // If this request has canceled, finish it and don‘t deliver.
            if (mRequest.isCanceled()) {
                mRequest.finish("canceled-at-delivery");
                return;
            }

            // Deliver a normal response or error, depending.
            if (mResponse.isSuccess()) {
                mRequest.deliverResponse(mResponse.result);
            } else {
                mRequest.deliverError(mResponse.error);
            }

            // If this is an intermediate response, add a marker, otherwise we‘re done
            // and the request can be finished.
            if (mResponse.intermediate) {
                mRequest.addMarker("intermediate-response");
            } else {
                mRequest.finish("done");
            }

            // If we have been provided a post-delivery runnable, run it.
            if (mRunnable != null) {
                mRunnable.run();
            }
       }
    }
这里我们可以看到,如果请求的响应是成功的,会去调用request的deliverResponse,这样一个完整的请求响应过程就完成了。
细心的你可能已经发现,从RequestQueue的add()方法开始,到NetworkDispatcher的run()方法结束,很多地方调用了request.addMarker("**-**-**");这个方法,通过里面的字符串,我们可以方便的理解整个处理流程:add-to-queue、network-queue-take、network-http-complete、network-parse-complete、network-cache-written、post-response/post-error、intermediate-response,另外还有request.finish("***-***");这个也可以帮助我们理解整个处理流程。

如上图(这个图我想修改下,但是不知道怎么改,都是拖出来的)。

3、请求的细节

从NetworkDispatcher的run()开始(代码上方有贴)。

3.1 第一次取消

                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;
                }
此时状态是刚从队列中取出:network-queue-take。首先,判断此请求是否已经取消,若取消直接调用request的finish()方法。这个时候我们会想到,怎么知道已经取消,或者用户怎么取消请求呢?到request中我们可以找到一个cancel()方法,就是通过此方法来取消请求的。注销 这个方法,我们可以看到在RequestQueue中有调用此方法,它们是:
    /**
     * Cancels all requests in this queue for which the given filter applies.
     * @param filter The filtering function to use
     */
    public void cancelAll(RequestFilter filter) {
        synchronized (mCurrentRequests) {
            for (Request<?> request : mCurrentRequests) {
                if (filter.apply(request)) {
                    request.cancel();
                }
            }
        }
    }

    /**
     * Cancels all requests in this queue with the given tag. Tag must be non-null
     * and equality is by identity.
     */
    public void cancelAll(final Object tag) {
        if (tag == null) {
            throw new IllegalArgumentException("Cannot cancelAll with a null tag");
        }
        cancelAll(new RequestFilter() {
            @Override
            public boolean apply(Request<?> request) {
                return request.getTag() == tag;
            }
        });
    }
这样,我们就可以知道,取消一个请求可以通过RequestQueue中的cancelAll()方法进行。这个时候取消的话,我们的请求是不会发出去的。当然,取消在任何时候都会发生,下面还会讲到,请求返回后取消的话结果是不会传递给用户的。

3.2 发起网络请求

                // Perform the network request.
                NetworkResponse networkResponse = mNetwork.performRequest(request);
                request.addMarker("network-http-complete");
通过执行network的performRequest()方法拿到一个请求结果。执行完这个时候,我们就得到了请求的结果。此处是可以自定义的,我们看下默认实现:
    @Override
    public NetworkResponse performRequest(Request<?> request) throws VolleyError {
        long requestStart = SystemClock.elapsedRealtime();
        while (true) {
            HttpResponse httpResponse = null;
            byte[] responseContents = null;
            Map<String, String> responseHeaders = new HashMap<String, String>();
            try {
                // Gather headers.
                Map<String, String> headers = new HashMap<String, String>();
                addCacheHeaders(headers, request.getCacheEntry());
                httpResponse = mHttpStack.performRequest(request, headers);
                StatusLine statusLine = httpResponse.getStatusLine();
                int statusCode = statusLine.getStatusCode();

                responseHeaders = convertHeaders(httpResponse.getAllHeaders());
                // Handle cache validation.
                if (statusCode == HttpStatus.SC_NOT_MODIFIED) {
                    return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED,
                            request.getCacheEntry().data, responseHeaders, true);
                }

                // Some responses such as 204s do not have content.  We must check.
                if (httpResponse.getEntity() != null) {
                  responseContents = entityToBytes(httpResponse.getEntity());
                } else {
                  // Add 0 byte response as a way of honestly representing a
                  // no-content request.
                  responseContents = new byte[0];
                }

                // if the request is slow, log it.
                long requestLifetime = SystemClock.elapsedRealtime() - requestStart;
                logSlowRequests(requestLifetime, request, responseContents, statusLine);

                if (statusCode < 200 || statusCode > 299) {
                    throw new IOException();
                }
                return new NetworkResponse(statusCode, responseContents, responseHeaders, false);
            } catch (SocketTimeoutException e) {
                attemptRetryOnException("socket", request, new TimeoutError());
            } catch (ConnectTimeoutException e) {
                attemptRetryOnException("connection", request, new TimeoutError());
            } catch (MalformedURLException e) {
                throw new RuntimeException("Bad URL " + request.getUrl(), e);
            } catch (IOException e) {
                int statusCode = 0;
                NetworkResponse networkResponse = null;
                if (httpResponse != null) {
                    statusCode = httpResponse.getStatusLine().getStatusCode();
                } else {
                    throw new NoConnectionError(e);
                }
                VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());
                if (responseContents != null) {
                    networkResponse = new NetworkResponse(statusCode, responseContents,
                            responseHeaders, false);
                    if (statusCode == HttpStatus.SC_UNAUTHORIZED ||
                            statusCode == HttpStatus.SC_FORBIDDEN) {
                        attemptRetryOnException("auth",
                                request, new AuthFailureError(networkResponse));
                    } else {
                        // TODO: Only throw ServerError for 5xx status codes.
                        throw new ServerError(networkResponse);
                    }
                } else {
                    throw new NetworkError(networkResponse);
                }
            }
        }
    }
关键部分:httpResponse = mHttpStack.performRequest(request, headers);通过这个执行网络请求。具体的细节还是可以自定义的。代码贴的太多就不继续贴了,两个实现大家可以具体去看看,比如网络请求超时时间的设置都是通过:request.getTimeoutMs();得到的。再到Request中你就可以知道设置超时是通过下面代码实现的:
/**
     * Sets the retry policy for this request.
     */
    public void setRetryPolicy(RetryPolicy retryPolicy) {
        mRetryPolicy = retryPolicy;
    }

3.3 解析请求结果

		// Parse the response here on the worker thread.
                Response<?> response = request.parseNetworkResponse(networkResponse);
                request.addMarker("network-parse-complete");
这个是不是很熟悉,我们在request中实现的parseNetworkResponse()。

3.4 最后结果的处理

这就说了,大家可以继续看默认的代码实现。值得提的是,在这里我们会发现,又一次调用了mRequest.isCanceled()决定是否将结果传递给用户。

4、总结

Volley.java 这个类的目的很明确,就是获取一个RequestQueue对象。从代码中可以看出,为了达到这个目的,我们至少要创建三个关键对象,Cache、Network、HttpStack。Cache是一个处理响应的缓存;Network是执行网络请求的接口;HttpStack是真正的执行网络请求的接口。Network和HttpStack都是接口,这样volley给我们预留了充足的自定义空间。如果完全自定义,我们可以创建自己的HttpStack和Network实现,这样我们就可以不再使用volley.java这个类了。

这篇文章写了半个月,也算完整的伴随最近的感情变迁。虽然后面写的越来越糟糕,不过现在终于写完了。欢迎各位指教~~


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。