FileDocCategorySizeDatePackage
RequestQueue.javaAPI DocAndroid 5.1 API18063Thu Mar 12 22:22:10 GMT 2015android.net.http

RequestQueue

public class RequestQueue extends Object implements RequestFeeder
{@hide}

Fields Summary
private final LinkedHashMap
mPending
Requests, indexed by HttpHost (scheme, host, port)
private final android.content.Context
mContext
private final ActivePool
mActivePool
private final android.net.ConnectivityManager
mConnectivityManager
private HttpHost
mProxyHost
private android.content.BroadcastReceiver
mProxyChangeReceiver
private static final int
CONNECTION_COUNT
Constructors Summary
public RequestQueue(android.content.Context context)
A RequestQueue class instance maintains a set of queued requests. It orders them, makes the requests against HTTP servers, and makes callbacks to supplied eventHandlers as data is read. It supports request prioritization, connection reuse and pipelining.

param
context application context

        this(context, CONNECTION_COUNT);
    
public RequestQueue(android.content.Context context, int connectionCount)
A RequestQueue class instance maintains a set of queued requests. It orders them, makes the requests against HTTP servers, and makes callbacks to supplied eventHandlers as data is read. It supports request prioritization, connection reuse and pipelining.

param
context application context
param
connectionCount The number of simultaneous connections

        mContext = context;

        mPending = new LinkedHashMap<HttpHost, LinkedList<Request>>(32);

        mActivePool = new ActivePool(connectionCount);
        mActivePool.startup();

        mConnectivityManager = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);
    
Methods Summary
private org.apache.http.HttpHostdetermineHost(org.apache.http.HttpHost host)

        // There used to be a comment in ConnectionThread about t-mob's proxy
        // being really bad about https. But, HttpsConnection actually looks
        // for a proxy and connects through it anyway. I think that this check
        // is still valid because if a site is https, we will use
        // HttpsConnection rather than HttpConnection if the proxy address is
        // not secure.
        return (mProxyHost == null || "https".equals(host.getSchemeName()))
                ? host : mProxyHost;
    
public synchronized voiddisablePlatformNotifications()
If platform notifications have been enabled, call this method to disable before destroying RequestQueue

        if (HttpLog.LOGV) HttpLog.v("RequestQueue.disablePlatformNotifications() network");

        if (mProxyChangeReceiver != null) {
            mContext.unregisterReceiver(mProxyChangeReceiver);
            mProxyChangeReceiver = null;
        }
    
synchronized voiddump()
debug tool: prints request queue to log

        HttpLog.v("dump()");
        StringBuilder dump = new StringBuilder();
        int count = 0;
        Iterator<Map.Entry<HttpHost, LinkedList<Request>>> iter;

        // mActivePool.log(dump);

        if (!mPending.isEmpty()) {
            iter = mPending.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry<HttpHost, LinkedList<Request>> entry = iter.next();
                String hostName = entry.getKey().getHostName();
                StringBuilder line = new StringBuilder("p" + count++ + " " + hostName + " ");

                LinkedList<Request> reqList = entry.getValue();
                ListIterator reqIter = reqList.listIterator(0);
                while (iter.hasNext()) {
                    Request request = (Request)iter.next();
                    line.append(request + " ");
                }
                dump.append(line);
                dump.append("\n");
            }
        }
        HttpLog.v(dump.toString());
    
public synchronized voidenablePlatformNotifications()
Enables data state and proxy tracking

        if (HttpLog.LOGV) HttpLog.v("RequestQueue.enablePlatformNotifications() network");

        if (mProxyChangeReceiver == null) {
            mProxyChangeReceiver =
                    new BroadcastReceiver() {
                        @Override
                        public void onReceive(Context ctx, Intent intent) {
                            setProxyConfig();
                        }
                    };
            mContext.registerReceiver(mProxyChangeReceiver,
                                      new IntentFilter(Proxy.PROXY_CHANGE_ACTION));
        }
        // we need to resample the current proxy setup
        setProxyConfig();
    
public org.apache.http.HttpHostgetProxyHost()
used by webkit

return
proxy host if set, null otherwise

        return mProxyHost;
    
public synchronized RequestgetRequest()

        Request ret = null;

        if (!mPending.isEmpty()) {
            ret = removeFirst(mPending);
        }
        if (HttpLog.LOGV) HttpLog.v("RequestQueue.getRequest() => " + ret);
        return ret;
    
public synchronized RequestgetRequest(org.apache.http.HttpHost host)

return
a request for given host if possible

        Request ret = null;

        if (mPending.containsKey(host)) {
            LinkedList<Request> reqList = mPending.get(host);
            ret = reqList.removeFirst();
            if (reqList.isEmpty()) {
                mPending.remove(host);
            }
        }
        if (HttpLog.LOGV) HttpLog.v("RequestQueue.getRequest(" + host + ") => " + ret);
        return ret;
    
public synchronized booleanhaveRequest(org.apache.http.HttpHost host)

return
true if a request for this host is available

        return mPending.containsKey(host);
    
protected synchronized voidqueueRequest(Request request, boolean head)

        HttpHost host = request.mProxyHost == null ? request.mHost : request.mProxyHost;
        LinkedList<Request> reqList;
        if (mPending.containsKey(host)) {
            reqList = mPending.get(host);
        } else {
            reqList = new LinkedList<Request>();
            mPending.put(host, reqList);
        }
        if (head) {
            reqList.addFirst(request);
        } else {
            reqList.add(request);
        }
    
public RequestHandlequeueRequest(java.lang.String url, java.lang.String method, java.util.Map headers, EventHandler eventHandler, java.io.InputStream bodyProvider, int bodyLength)
Queues an HTTP request

param
url The url to load.
param
method "GET" or "POST."
param
headers A hashmap of http headers.
param
eventHandler The event handler for handling returned data. Callbacks will be made on the supplied instance.
param
bodyProvider InputStream providing HTTP body, null if none
param
bodyLength length of body, must be 0 if bodyProvider is null

        WebAddress uri = new WebAddress(url);
        return queueRequest(url, uri, method, headers, eventHandler,
                            bodyProvider, bodyLength);
    
public RequestHandlequeueRequest(java.lang.String url, android.net.WebAddress uri, java.lang.String method, java.util.Map headers, EventHandler eventHandler, java.io.InputStream bodyProvider, int bodyLength)
Queues an HTTP request

param
url The url to load.
param
uri The uri of the url to load.
param
method "GET" or "POST."
param
headers A hashmap of http headers.
param
eventHandler The event handler for handling returned data. Callbacks will be made on the supplied instance.
param
bodyProvider InputStream providing HTTP body, null if none
param
bodyLength length of body, must be 0 if bodyProvider is null


        if (HttpLog.LOGV) HttpLog.v("RequestQueue.queueRequest " + uri);

        // Ensure there is an eventHandler set
        if (eventHandler == null) {
            eventHandler = new LoggingEventHandler();
        }

        /* Create and queue request */
        Request req;
        HttpHost httpHost = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());

        // set up request
        req = new Request(method, httpHost, mProxyHost, uri.getPath(), bodyProvider,
                          bodyLength, eventHandler, headers);

        queueRequest(req, false);

        mActivePool.mTotalRequest++;

        // dump();
        mActivePool.startConnectionThread();

        return new RequestHandle(
                this, url, uri, method, headers, bodyProvider, bodyLength,
                req);
    
public RequestHandlequeueSynchronousRequest(java.lang.String url, android.net.WebAddress uri, java.lang.String method, java.util.Map headers, EventHandler eventHandler, java.io.InputStream bodyProvider, int bodyLength)

        if (HttpLog.LOGV) {
            HttpLog.v("RequestQueue.dispatchSynchronousRequest " + uri);
        }

        HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());

        Request req = new Request(method, host, mProxyHost, uri.getPath(),
                bodyProvider, bodyLength, eventHandler, headers);

        // Open a new connection that uses our special RequestFeeder
        // implementation.
        host = determineHost(host);
        Connection conn = Connection.getConnection(mContext, host, mProxyHost,
                new SyncFeeder());

        // TODO: I would like to process the request here but LoadListener
        // needs a RequestHandle to process some messages.
        return new RequestHandle(this, url, uri, method, headers, bodyProvider,
                bodyLength, req, conn);

    
private RequestremoveFirst(java.util.LinkedHashMap requestQueue)

        Request ret = null;
        Iterator<Map.Entry<HttpHost, LinkedList<Request>>> iter = requestQueue.entrySet().iterator();
        if (iter.hasNext()) {
            Map.Entry<HttpHost, LinkedList<Request>> entry = iter.next();
            LinkedList<Request> reqList = entry.getValue();
            ret = reqList.removeFirst();
            if (reqList.isEmpty()) {
                requestQueue.remove(entry.getKey());
            }
        }
        return ret;
    
synchronized booleanrequestsPending()

return
true iff there are any non-active requests pending

        return !mPending.isEmpty();
    
public voidrequeueRequest(Request request)
Put request back on head of queue

        queueRequest(request, true);
    
private synchronized voidsetProxyConfig()
Because our IntentReceiver can run within a different thread, synchronize setting the proxy

        NetworkInfo info = mConnectivityManager.getActiveNetworkInfo();
        if (info != null && info.getType() == ConnectivityManager.TYPE_WIFI) {
            mProxyHost = null;
        } else {
            String host = Proxy.getHost(mContext);
            if (HttpLog.LOGV) HttpLog.v("RequestQueue.setProxyConfig " + host);
            if (host == null) {
                mProxyHost = null;
            } else {
                mActivePool.disablePersistence();
                mProxyHost = new HttpHost(host, Proxy.getPort(mContext), "http");
            }
        }
    
public voidshutdown()
This must be called to cleanly shutdown RequestQueue

        mActivePool.shutdown();
    
public voidstartTiming()

        mActivePool.startTiming();
    
public voidstopTiming()

        mActivePool.stopTiming();