diff options
Diffstat (limited to 'src/main/java/com/android/volley/toolbox/HttpClientStack.java')
-rw-r--r-- | src/main/java/com/android/volley/toolbox/HttpClientStack.java | 113 |
1 files changed, 57 insertions, 56 deletions
diff --git a/src/main/java/com/android/volley/toolbox/HttpClientStack.java b/src/main/java/com/android/volley/toolbox/HttpClientStack.java index 023ee21..be0918a 100644 --- a/src/main/java/com/android/volley/toolbox/HttpClientStack.java +++ b/src/main/java/com/android/volley/toolbox/HttpClientStack.java @@ -19,7 +19,11 @@ package com.android.volley.toolbox; import com.android.volley.AuthFailureError; import com.android.volley.Request; import com.android.volley.Request.Method; - +import java.io.IOException; +import java.net.URI; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; @@ -38,23 +42,17 @@ import org.apache.http.message.BasicNameValuePair; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; -import java.io.IOException; -import java.net.URI; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - /** * An HttpStack that performs request over an {@link HttpClient}. * * @deprecated The Apache HTTP library on Android is deprecated. Use {@link HurlStack} or another - * {@link BaseHttpStack} implementation. + * {@link BaseHttpStack} implementation. */ @Deprecated public class HttpClientStack implements HttpStack { protected final HttpClient mClient; - private final static String HEADER_CONTENT_TYPE = "Content-Type"; + private static final String HEADER_CONTENT_TYPE = "Content-Type"; public HttpClientStack(HttpClient client) { mClient = client; @@ -68,7 +66,7 @@ public class HttpClientStack implements HttpStack { @SuppressWarnings("unused") private static List<NameValuePair> getPostParameterPairs(Map<String, String> postParams) { - List<NameValuePair> result = new ArrayList<NameValuePair>(postParams.size()); + List<NameValuePair> result = new ArrayList<>(postParams.size()); for (String key : postParams.keySet()) { result.add(new BasicNameValuePair(key, postParams.get(key))); } @@ -91,64 +89,70 @@ public class HttpClientStack implements HttpStack { return mClient.execute(httpRequest); } - /** - * Creates the appropriate subclass of HttpUriRequest for passed in request. - */ + /** Creates the appropriate subclass of HttpUriRequest for passed in request. */ @SuppressWarnings("deprecation") - /* protected */ static HttpUriRequest createHttpRequest(Request<?> request, - Map<String, String> additionalHeaders) throws AuthFailureError { + /* protected */ static HttpUriRequest createHttpRequest( + Request<?> request, Map<String, String> additionalHeaders) throws AuthFailureError { 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.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.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; - } + 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 void setEntityIfNonEmptyBody(HttpEntityEnclosingRequestBase httpRequest, - Request<?> request) throws AuthFailureError { + private static void setEntityIfNonEmptyBody( + HttpEntityEnclosingRequestBase httpRequest, Request<?> request) + throws AuthFailureError { byte[] body = request.getBody(); if (body != null) { HttpEntity entity = new ByteArrayEntity(body); @@ -159,7 +163,7 @@ public class HttpClientStack implements HttpStack { /** * Called before the request is executed using the underlying HttpClient. * - * <p>Overwrite in subclasses to augment the request.</p> + * <p>Overwrite in subclasses to augment the request. */ protected void onPrepareRequest(HttpUriRequest request) throws IOException { // Nothing. @@ -170,7 +174,7 @@ public class HttpClientStack implements HttpStack { */ public static final class HttpPatch extends HttpEntityEnclosingRequestBase { - public final static String METHOD_NAME = "PATCH"; + public static final String METHOD_NAME = "PATCH"; public HttpPatch() { super(); @@ -181,9 +185,7 @@ public class HttpClientStack implements HttpStack { setURI(uri); } - /** - * @throws IllegalArgumentException if the uri is invalid. - */ + /** @throws IllegalArgumentException if the uri is invalid. */ public HttpPatch(final String uri) { super(); setURI(URI.create(uri)); @@ -193,6 +195,5 @@ public class HttpClientStack implements HttpStack { public String getMethod() { return METHOD_NAME; } - } } |