FileDocCategorySizeDatePackage
HttpClientStack.javaAPI DocAndroid 5.1 API7484Thu Mar 12 22:22:56 GMT 2015com.android.volley.toolbox

HttpClientStack

public class HttpClientStack extends Object implements HttpStack
An HttpStack that performs request over an {@link HttpClient}.

Fields Summary
protected final HttpClient
mClient
private static final String
HEADER_CONTENT_TYPE
Constructors Summary
public HttpClientStack(HttpClient client)


       
        mClient = client;
    
Methods Summary
private static voidaddHeaders(org.apache.http.client.methods.HttpUriRequest httpRequest, java.util.Map headers)

        for (String key : headers.keySet()) {
            httpRequest.setHeader(key, headers.get(key));
        }
    
static org.apache.http.client.methods.HttpUriRequestcreateHttpRequest(com.android.volley.Request request, java.util.Map additionalHeaders)
Creates the appropriate subclass of HttpUriRequest for passed in request.

        switch (request.getMethod()) {
            case Method.DEPRECATED_GET_OR_POST: {
                // This is the deprecated way that needs to be handled for backwards compatibility.
                // If the request's post body is null, then the assumption is that the request is
                // GET.  Otherwise, it is assumed that the request is a POST.
                byte[] postBody = request.getPostBody();
                if (postBody != null) {
                    HttpPost postRequest = new HttpPost(request.getUrl());
                    postRequest.addHeader(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
                    HttpEntity entity;
                    entity = new ByteArrayEntity(postBody);
                    postRequest.setEntity(entity);
                    return postRequest;
                } else {
                    return new HttpGet(request.getUrl());
                }
            }
            case Method.GET:
                return new HttpGet(request.getUrl());
            case Method.DELETE:
                return new HttpDelete(request.getUrl());
            case Method.POST: {
                HttpPost postRequest = new HttpPost(request.getUrl());
                postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
                setEntityIfNonEmptyBody(postRequest, request);
                return postRequest;
            }
            case Method.PUT: {
                HttpPut putRequest = new HttpPut(request.getUrl());
                putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
                setEntityIfNonEmptyBody(putRequest, request);
                return putRequest;
            }
            case Method.HEAD:
                return new HttpHead(request.getUrl());
            case Method.OPTIONS:
                return new HttpOptions(request.getUrl());
            case Method.TRACE:
                return new HttpTrace(request.getUrl());
            case Method.PATCH: {
                HttpPatch patchRequest = new HttpPatch(request.getUrl());
                patchRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
                setEntityIfNonEmptyBody(patchRequest, request);
                return patchRequest;
            }
            default:
                throw new IllegalStateException("Unknown request method.");
        }
    
private static java.util.ListgetPostParameterPairs(java.util.Map postParams)

        List<NameValuePair> result = new ArrayList<NameValuePair>(postParams.size());
        for (String key : postParams.keySet()) {
            result.add(new BasicNameValuePair(key, postParams.get(key)));
        }
        return result;
    
protected voidonPrepareRequest(org.apache.http.client.methods.HttpUriRequest request)
Called before the request is executed using the underlying HttpClient.

Overwrite in subclasses to augment the request.

        // Nothing.
    
public org.apache.http.HttpResponseperformRequest(com.android.volley.Request request, java.util.Map additionalHeaders)

        HttpUriRequest httpRequest = createHttpRequest(request, additionalHeaders);
        addHeaders(httpRequest, additionalHeaders);
        addHeaders(httpRequest, request.getHeaders());
        onPrepareRequest(httpRequest);
        HttpParams httpParams = httpRequest.getParams();
        int timeoutMs = request.getTimeoutMs();
        // TODO: Reevaluate this connection timeout based on more wide-scale
        // data collection and possibly different for wifi vs. 3G.
        HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
        HttpConnectionParams.setSoTimeout(httpParams, timeoutMs);
        return mClient.execute(httpRequest);
    
private static voidsetEntityIfNonEmptyBody(org.apache.http.client.methods.HttpEntityEnclosingRequestBase httpRequest, com.android.volley.Request request)

        byte[] body = request.getBody();
        if (body != null) {
            HttpEntity entity = new ByteArrayEntity(body);
            httpRequest.setEntity(entity);
        }