public final class

AndroidHttpClient

extends Object
implements HttpClient
java.lang.Object
   ↳ android.net.http.AndroidHttpClient

Class Overview

Subclass of the Apache DefaultHttpClient that is configured with reasonable default settings and registered schemes for Android, and also lets the user add HttpRequestInterceptor classes. Don't create this directly, use the newInstance(String) factory method.

This client processes cookies but does not retain them by default. To retain cookies, simply add a cookie store to the HttpContext:

context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

Summary

Fields
public static long DEFAULT_SYNC_MIN_GZIP_BYTES
Public Methods
void close()
Release resources associated with this client.
void disableCurlLogging()
Disables cURL logging for this client.
void enableCurlLogging(String name, int level)
Enables cURL request logging for this client.
HttpResponse execute(HttpUriRequest request)
Executes a request using the default context.
<T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context)
Executes a request to the target using the given context and processes the response using the given response handler.
HttpResponse execute(HttpUriRequest request, HttpContext context)
Executes a request using the given context.
<T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler)
Executes a request using the default context and processes the response using the given response handler.
<T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler)
Executes a request to the target using the default context and processes the response using the given response handler.
<T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context)
Executes a request using the given context and processes the response using the given response handler.
HttpResponse execute(HttpHost target, HttpRequest request)
Executes a request to the target using the default context.
HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context)
Executes a request to the target using the given context.
static AbstractHttpEntity getCompressedEntity(byte[] data, ContentResolver resolver)
Compress data to send to server.
ClientConnectionManager getConnectionManager()
Obtains the connection manager used by this client.
static long getMinGzipSize(ContentResolver resolver)
Retrieves the minimum size for compressing data.
HttpParams getParams()
Obtains the parameters for this client.
static InputStream getUngzippedContent(HttpEntity entity)
Gets the input stream from a response entity.
static void modifyRequestToAcceptGzipResponse(HttpRequest request)
Modifies a request to indicate to the server that we would like a gzipped response.
static AndroidHttpClient newInstance(String userAgent, Context context)
Create a new HttpClient with reasonable defaults (which you can update).
static AndroidHttpClient newInstance(String userAgent)
Create a new HttpClient with reasonable defaults (which you can update).
static long parseDate(String dateString)
Returns the date of the given HTTP date string.
Protected Methods
void finalize()
Called before the object's memory is reclaimed by the VM.
[Expand]
Inherited Methods
From class java.lang.Object
From interface org.apache.http.client.HttpClient

Fields

public static long DEFAULT_SYNC_MIN_GZIP_BYTES

Since: API Level 8

Public Methods

public void close ()

Since: API Level 8

Release resources associated with this client. You must call this, or significant resources (sockets and memory) may be leaked.

public void disableCurlLogging ()

Since: API Level 8

Disables cURL logging for this client.

public void enableCurlLogging (String name, int level)

Since: API Level 8

Enables cURL request logging for this client.

Parameters
name to log messages with
level at which to log messages (see Log)

public HttpResponse execute (HttpUriRequest request)

Since: API Level 8

Executes a request using the default context.

Parameters
request the request to execute
Returns
  • the response to the request. This is always a final response, never an intermediate response with an 1xx status code. Whether redirects or authentication challenges will be returned or handled automatically depends on the implementation and configuration of this client.
Throws
IOException

public T execute (HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context)

Since: API Level 8

Executes a request to the target using the given context and processes the response using the given response handler.

Parameters
target the target host for the request. Implementations may accept null if they can still determine a route, for example to a default target or by inspecting the request.
request the request to execute
responseHandler the response handler
context the context to use for the execution, or null to use the default context
Returns
  • the response object as generated by the response handler.

public HttpResponse execute (HttpUriRequest request, HttpContext context)

Since: API Level 8

Executes a request using the given context. The route to the target will be determined by the HTTP client.

Parameters
request the request to execute
context the context to use for the execution, or null to use the default context
Returns
  • the response to the request. This is always a final response, never an intermediate response with an 1xx status code. Whether redirects or authentication challenges will be returned or handled automatically depends on the implementation and configuration of this client.
Throws
IOException

public T execute (HttpUriRequest request, ResponseHandler<? extends T> responseHandler)

Since: API Level 8

Executes a request using the default context and processes the response using the given response handler.

Parameters
request the request to execute
responseHandler the response handler
Returns
  • the response object as generated by the response handler.

public T execute (HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler)

Since: API Level 8

Executes a request to the target using the default context and processes the response using the given response handler.

Parameters
target the target host for the request. Implementations may accept null if they can still determine a route, for example to a default target or by inspecting the request.
request the request to execute
responseHandler the response handler
Returns
  • the response object as generated by the response handler.

public T execute (HttpUriRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context)

Since: API Level 8

Executes a request using the given context and processes the response using the given response handler.

Parameters
request the request to execute
responseHandler the response handler
Returns
  • the response object as generated by the response handler.

public HttpResponse execute (HttpHost target, HttpRequest request)

Since: API Level 8

Executes a request to the target using the default context.

Parameters
target the target host for the request. Implementations may accept null if they can still determine a route, for example to a default target or by inspecting the request.
request the request to execute
Returns
  • the response to the request. This is always a final response, never an intermediate response with an 1xx status code. Whether redirects or authentication challenges will be returned or handled automatically depends on the implementation and configuration of this client.
Throws
IOException

public HttpResponse execute (HttpHost target, HttpRequest request, HttpContext context)

Since: API Level 8

Executes a request to the target using the given context.

Parameters
target the target host for the request. Implementations may accept null if they can still determine a route, for example to a default target or by inspecting the request.
request the request to execute
context the context to use for the execution, or null to use the default context
Returns
  • the response to the request. This is always a final response, never an intermediate response with an 1xx status code. Whether redirects or authentication challenges will be returned or handled automatically depends on the implementation and configuration of this client.
Throws
IOException

public static AbstractHttpEntity getCompressedEntity (byte[] data, ContentResolver resolver)

Since: API Level 8

Compress data to send to server. Creates a Http Entity holding the gzipped data. The data will not be compressed if it is too short.

Parameters
data The bytes to compress
Returns
  • Entity holding the data
Throws
IOException

public ClientConnectionManager getConnectionManager ()

Since: API Level 8

Obtains the connection manager used by this client.

Returns
  • the connection manager

public static long getMinGzipSize (ContentResolver resolver)

Since: API Level 8

Retrieves the minimum size for compressing data. Shorter data will not be compressed.

public HttpParams getParams ()

Since: API Level 8

Obtains the parameters for this client. These parameters will become defaults for all requests being executed with this client, and for the parameters of dependent objects in this client.

Returns
  • the default parameters

public static InputStream getUngzippedContent (HttpEntity entity)

Since: API Level 8

Gets the input stream from a response entity. If the entity is gzipped then this will get a stream over the uncompressed data.

Parameters
entity the entity whose content should be read
Returns
  • the input stream to read from
Throws
IOException

public static void modifyRequestToAcceptGzipResponse (HttpRequest request)

Since: API Level 8

Modifies a request to indicate to the server that we would like a gzipped response. (Uses the "Accept-Encoding" HTTP header.)

Parameters
request the request to modify

public static AndroidHttpClient newInstance (String userAgent, Context context)

Since: API Level 8

Create a new HttpClient with reasonable defaults (which you can update).

Parameters
userAgent to report in your HTTP requests
context to use for caching SSL sessions (may be null for no caching)
Returns
  • AndroidHttpClient for you to use for all your requests.

public static AndroidHttpClient newInstance (String userAgent)

Since: API Level 8

Create a new HttpClient with reasonable defaults (which you can update).

Parameters
userAgent to report in your HTTP requests.
Returns
  • AndroidHttpClient for you to use for all your requests.

public static long parseDate (String dateString)

Since: API Level 8

Returns the date of the given HTTP date string. This method can identify and parse the date formats emitted by common HTTP servers, such as RFC 822, RFC 850, RFC 1036, RFC 1123 and ANSI C's asctime().

Returns
  • the number of milliseconds since Jan. 1, 1970, midnight GMT.
Throws
IllegalArgumentException if dateString is not a date or of an unsupported format.

Protected Methods

protected void finalize ()

Since: API Level 8

Called before the object's memory is reclaimed by the VM. This can only happen once the garbage collector has detected that the object is no longer reachable by any thread of the running application.

The method can be used to free system resources or perform other cleanup before the object is garbage collected. The default implementation of the method is empty, which is also expected by the VM, but subclasses can override finalize() as required. Uncaught exceptions which are thrown during the execution of this method cause it to terminate immediately but are otherwise ignored.

Note that the VM does guarantee that finalize() is called at most once for any object, but it doesn't guarantee when (if at all) finalize() will be called. For example, object B's finalize() can delay the execution of object A's finalize() method and therefore it can delay the reclamation of A's memory. To be safe, use a ReferenceQueue, because it provides more control over the way the VM deals with references during garbage collection.

Throws
Throwable