Android APIs
public final class

AndroidHttpClient

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

Class Overview

Implementation of the Apache DefaultHttpClient that is configured with reasonable default settings and registered schemes for Android. 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()
Invoked when the garbage collector has detected that this instance is no longer reachable.
[Expand]
Inherited Methods
From class java.lang.Object
From interface org.apache.http.client.HttpClient

Fields

public static long DEFAULT_SYNC_MIN_GZIP_BYTES

Added in API level 8

Public Methods

public void close ()

Added in 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 ()

Added in API level 8

Disables cURL logging for this client.

public void enableCurlLogging (String name, int level)

Added in 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)

Added in 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)

Added in 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)

Added in 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)

Added in 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)

Added in 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)

Added in 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)

Added in 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)

Added in 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)

Added in 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 ()

Added in API level 8

Obtains the connection manager used by this client.

Returns
  • the connection manager

public static long getMinGzipSize (ContentResolver resolver)

Added in API level 8

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

public HttpParams getParams ()

Added in 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)

Added in 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)

Added in 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)

Added in 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)

Added in 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)

Added in 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 ()

Added in API level 8

Invoked when the garbage collector has detected that this instance is no longer reachable. The default implementation does nothing, but this method can be overridden to free resources.

Note that objects that override finalize are significantly more expensive than objects that don't. Finalizers may be run a long time after the object is no longer reachable, depending on memory pressure, so it's a bad idea to rely on them for cleanup. Note also that finalizers are run on a single VM-wide finalizer thread, so doing blocking work in a finalizer is a bad idea. A finalizer is usually only necessary for a class that has a native peer and needs to call a native method to destroy that peer. Even then, it's better to provide an explicit close method (and implement Closeable), and insist that callers manually dispose of instances. This works well for something like files, but less well for something like a BigInteger where typical calling code would have to deal with lots of temporaries. Unfortunately, code that creates lots of temporaries is the worst kind of code from the point of view of the single finalizer thread.

If you must use finalizers, consider at least providing your own ReferenceQueue and having your own thread process that queue.

Unlike constructors, finalizers are not automatically chained. You are responsible for calling super.finalize() yourself.

Uncaught exceptions thrown by finalizers are ignored and do not terminate the finalizer thread. See Effective Java Item 7, "Avoid finalizers" for more.

Throws
Throwable