FileDocCategorySizeDatePackage
NetworkDispatcher.javaAPI DocAndroid 5.1 API5596Thu Mar 12 22:22:56 GMT 2015com.android.volley

NetworkDispatcher

public class NetworkDispatcher extends Thread
Provides a thread for performing network dispatch from a queue of requests. Requests added to the specified queue are processed from the network via a specified {@link Network} interface. Responses are committed to cache, if eligible, using a specified {@link Cache} interface. Valid responses and errors are posted back to the caller via a {@link ResponseDelivery}.

Fields Summary
private final BlockingQueue
mQueue
The queue of requests to service.
private final Network
mNetwork
The network interface for processing requests.
private final Cache
mCache
The cache to write to.
private final ResponseDelivery
mDelivery
For posting responses and errors.
private volatile boolean
mQuit
Used for telling us to die.
Constructors Summary
public NetworkDispatcher(BlockingQueue queue, Network network, Cache cache, ResponseDelivery delivery)
Creates a new network dispatcher thread. You must call {@link #start()} in order to begin processing.

param
queue Queue of incoming requests for triage
param
network Network interface to use for performing requests
param
cache Cache interface to use for writing responses to cache
param
delivery Delivery interface to use for posting responses


                                                               
      
               
              
        mQueue = queue;
        mNetwork = network;
        mCache = cache;
        mDelivery = delivery;
    
Methods Summary
private voidaddTrafficStatsTag(Request request)

        // Tag the request (if API >= 14)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            TrafficStats.setThreadStatsTag(request.getTrafficStatsTag());
        }
    
private voidparseAndDeliverNetworkError(Request request, VolleyError error)

        error = request.parseNetworkError(error);
        mDelivery.postError(request, error);
    
public voidquit()
Forces this dispatcher to quit immediately. If any requests are still in the queue, they are not guaranteed to be processed.

        mQuit = true;
        interrupt();
    
public voidrun()

        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;
                }

                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));
            }
        }