com.loopj.android.http
Class AsyncHttpClient

java.lang.Object
  extended by com.loopj.android.http.AsyncHttpClient

public class AsyncHttpClient
extends Object

The AsyncHttpClient can be used to make asynchronous GET, POST, PUT and DELETE HTTP requests in your Android applications. Requests can be made with additional parameters by passing a RequestParams instance, and responses can be handled by passing an anonymously overridden AsyncHttpResponseHandler instance.

For example:

 AsyncHttpClient client = new AsyncHttpClient();
 client.get("http://www.google.com", new AsyncHttpResponseHandler() {
     @Override
     public void onSuccess(String response) {
         System.out.println(response);
     }
 });
 


Constructor Summary
AsyncHttpClient()
          Creates a new AsyncHttpClient.
 
Method Summary
 void cancelRequests(Context context, boolean mayInterruptIfRunning)
          Cancels any pending (or potentially active) requests associated with the passed Context.
 void delete(Context context, String url, AsyncHttpResponseHandler responseHandler)
          Perform a HTTP DELETE request.
 void delete(String url, AsyncHttpResponseHandler responseHandler)
          Perform a HTTP DELETE request.
 void get(Context context, String url, AsyncHttpResponseHandler responseHandler)
          Perform a HTTP GET request without any parameters and track the Android Context which initiated the request.
 void get(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler)
          Perform a HTTP GET request and track the Android Context which initiated the request.
 void get(String url, AsyncHttpResponseHandler responseHandler)
          Perform a HTTP GET request, without any parameters.
 void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler)
          Perform a HTTP GET request with parameters.
 HttpClient getHttpClient()
          Get the underlying HttpClient instance.
 void post(Context context, String url, HttpEntity entity, String contentType, AsyncHttpResponseHandler responseHandler)
          Perform a HTTP POST request and track the Android Context which initiated the request.
 void post(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler)
          Perform a HTTP POST request and track the Android Context which initiated the request.
 void post(String url, AsyncHttpResponseHandler responseHandler)
          Perform a HTTP POST request, without any parameters.
 void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler)
          Perform a HTTP POST request with parameters.
 void put(Context context, String url, HttpEntity entity, String contentType, AsyncHttpResponseHandler responseHandler)
          Perform a HTTP PUT request and track the Android Context which initiated the request.
 void put(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler)
          Perform a HTTP PUT request and track the Android Context which initiated the request.
 void put(String url, AsyncHttpResponseHandler responseHandler)
          Perform a HTTP PUT request, without any parameters.
 void put(String url, RequestParams params, AsyncHttpResponseHandler responseHandler)
          Perform a HTTP PUT request with parameters.
 void setCookieStore(CookieStore cookieStore)
          Sets an optional CookieStore to use when making requests
 void setSSLSocketFactory(SSLSocketFactory sslSocketFactory)
          Sets the SSLSocketFactory to user when making requests.
 void setThreadPool(ThreadPoolExecutor threadPool)
          Overrides the threadpool implementation used when queuing/pooling requests.
 void setUserAgent(String userAgent)
          Sets the User-Agent header to be sent with each request.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AsyncHttpClient

public AsyncHttpClient()
Creates a new AsyncHttpClient.

Method Detail

getHttpClient

public HttpClient getHttpClient()
Get the underlying HttpClient instance. This is useful for setting additional fine-grained settings for requests by accessing the client's ConnectionManager, HttpParams and SchemeRegistry.


setCookieStore

public void setCookieStore(CookieStore cookieStore)
Sets an optional CookieStore to use when making requests

Parameters:
cookieStore - The CookieStore implementation to use, usually an instance of PersistentCookieStore

setThreadPool

public void setThreadPool(ThreadPoolExecutor threadPool)
Overrides the threadpool implementation used when queuing/pooling requests. By default, Executors.newCachedThreadPool() is used.

Parameters:
threadPool - an instance of ThreadPoolExecutor to use for queuing/pooling requests.

setUserAgent

public void setUserAgent(String userAgent)
Sets the User-Agent header to be sent with each request. By default, "Android Asynchronous Http Client/VERSION (http://loopj.com/android-async-http/)" is used.

Parameters:
userAgent - the string to use in the User-Agent header.

setSSLSocketFactory

public void setSSLSocketFactory(SSLSocketFactory sslSocketFactory)
Sets the SSLSocketFactory to user when making requests. By default, a new, default SSLSocketFactory is used.

Parameters:
sslSocketFactory - the socket factory to use for https requests.

cancelRequests

public void cancelRequests(Context context,
                           boolean mayInterruptIfRunning)
Cancels any pending (or potentially active) requests associated with the passed Context.

Note: This will only affect requests which were created with a non-null android Context. This method is intended to be used in the onDestroy method of your android activities to destroy all requests which are no longer required.

Parameters:
context - the android Context instance associated to the request.
mayInterruptIfRunning - specifies if active requests should be cancelled along with pending requests.

get

public void get(String url,
                AsyncHttpResponseHandler responseHandler)
Perform a HTTP GET request, without any parameters.

Parameters:
url - the URL to send the request to.
responseHandler - the response handler instance that should handle the response.

get

public void get(String url,
                RequestParams params,
                AsyncHttpResponseHandler responseHandler)
Perform a HTTP GET request with parameters.

Parameters:
url - the URL to send the request to.
params - additional GET parameters to send with the request.
responseHandler - the response handler instance that should handle the response.

get

public void get(Context context,
                String url,
                AsyncHttpResponseHandler responseHandler)
Perform a HTTP GET request without any parameters and track the Android Context which initiated the request.

Parameters:
context - the Android Context which initiated the request.
url - the URL to send the request to.
responseHandler - the response handler instance that should handle the response.

get

public void get(Context context,
                String url,
                RequestParams params,
                AsyncHttpResponseHandler responseHandler)
Perform a HTTP GET request and track the Android Context which initiated the request.

Parameters:
context - the Android Context which initiated the request.
url - the URL to send the request to.
params - additional GET parameters to send with the request.
responseHandler - the response handler instance that should handle the response.

post

public void post(String url,
                 AsyncHttpResponseHandler responseHandler)
Perform a HTTP POST request, without any parameters.

Parameters:
url - the URL to send the request to.
responseHandler - the response handler instance that should handle the response.

post

public void post(String url,
                 RequestParams params,
                 AsyncHttpResponseHandler responseHandler)
Perform a HTTP POST request with parameters.

Parameters:
url - the URL to send the request to.
params - additional POST parameters or files to send with the request.
responseHandler - the response handler instance that should handle the response.

post

public void post(Context context,
                 String url,
                 RequestParams params,
                 AsyncHttpResponseHandler responseHandler)
Perform a HTTP POST request and track the Android Context which initiated the request.

Parameters:
context - the Android Context which initiated the request.
url - the URL to send the request to.
params - additional POST parameters or files to send with the request.
responseHandler - the response handler instance that should handle the response.

post

public void post(Context context,
                 String url,
                 HttpEntity entity,
                 String contentType,
                 AsyncHttpResponseHandler responseHandler)
Perform a HTTP POST request and track the Android Context which initiated the request.

Parameters:
context - the Android Context which initiated the request.
url - the URL to send the request to.
entity - a raw HttpEntity to send with the request, for example, use this to send string/json/xml payloads to a server by passing a StringEntity.
contentType - the content type of the payload you are sending, for example application/json if sending a json payload.
responseHandler - the response handler instance that should handle the response.

put

public void put(String url,
                AsyncHttpResponseHandler responseHandler)
Perform a HTTP PUT request, without any parameters.

Parameters:
url - the URL to send the request to.
responseHandler - the response handler instance that should handle the response.

put

public void put(String url,
                RequestParams params,
                AsyncHttpResponseHandler responseHandler)
Perform a HTTP PUT request with parameters.

Parameters:
url - the URL to send the request to.
params - additional PUT parameters or files to send with the request.
responseHandler - the response handler instance that should handle the response.

put

public void put(Context context,
                String url,
                RequestParams params,
                AsyncHttpResponseHandler responseHandler)
Perform a HTTP PUT request and track the Android Context which initiated the request.

Parameters:
context - the Android Context which initiated the request.
url - the URL to send the request to.
params - additional PUT parameters or files to send with the request.
responseHandler - the response handler instance that should handle the response.

put

public void put(Context context,
                String url,
                HttpEntity entity,
                String contentType,
                AsyncHttpResponseHandler responseHandler)
Perform a HTTP PUT request and track the Android Context which initiated the request.

Parameters:
context - the Android Context which initiated the request.
url - the URL to send the request to.
entity - a raw HttpEntity to send with the request, for example, use this to send string/json/xml payloads to a server by passing a StringEntity.
contentType - the content type of the payload you are sending, for example application/json if sending a json payload.
responseHandler - the response handler instance that should handle the response.

delete

public void delete(String url,
                   AsyncHttpResponseHandler responseHandler)
Perform a HTTP DELETE request.

Parameters:
url - the URL to send the request to.
responseHandler - the response handler instance that should handle the response.

delete

public void delete(Context context,
                   String url,
                   AsyncHttpResponseHandler responseHandler)
Perform a HTTP DELETE request.

Parameters:
context - the Android Context which initiated the request.
url - the URL to send the request to.
responseHandler - the response handler instance that should handle the response.