com.baidu.sapi2.http
类 AsyncHttpClient

java.lang.Object
  继承者 com.baidu.sapi2.http.AsyncHttpClient
直接已知子类:
SyncHttpClient

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);
     }
 });
 


构造方法摘要
AsyncHttpClient()
          Creates a new AsyncHttpClient.
 
方法摘要
 void addHeader(String header, String value)
          Sets headers that will be added to all requests this client makes (before sending).
 void cancelRequests(android.content.Context context, boolean mayInterruptIfRunning)
          Cancels any pending (or potentially active) requests associated with the passed Context.
 void delete(android.content.Context context, String url, AsyncHttpResponseHandler responseHandler)
          Perform a HTTP DELETE request.
 void delete(android.content.Context context, String url, org.apache.http.Header[] headers, AsyncHttpResponseHandler responseHandler)
          Perform a HTTP DELETE request.
 void delete(String url, AsyncHttpResponseHandler responseHandler)
          Perform a HTTP DELETE request.
 void get(android.content.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(android.content.Context context, String url, org.apache.http.Header[] headers, RequestParams params, AsyncHttpResponseHandler responseHandler)
          Perform a HTTP GET request and track the Android Context which initiated the request with customized headers
 void get(android.content.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.
 org.apache.http.client.HttpClient getHttpClient()
          Get the underlying HttpClient instance.
 org.apache.http.protocol.HttpContext getHttpContext()
          Get the underlying HttpContext instance.
static String getUrlWithQueryString(String url, RequestParams params)
           
 void post(android.content.Context context, String url, org.apache.http.Header[] headers, org.apache.http.HttpEntity entity, String contentType, AsyncHttpResponseHandler responseHandler)
          Perform a HTTP POST request and track the Android Context which initiated the request.
 void post(android.content.Context context, String url, org.apache.http.Header[] headers, RequestParams params, String contentType, AsyncHttpResponseHandler responseHandler)
          Perform a HTTP POST request and track the Android Context which initiated the request.
 void post(android.content.Context context, String url, org.apache.http.HttpEntity entity, String contentType, AsyncHttpResponseHandler responseHandler)
          Perform a HTTP POST request and track the Android Context which initiated the request.
 void post(android.content.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(android.content.Context context, String url, org.apache.http.Header[] headers, org.apache.http.HttpEntity entity, String contentType, AsyncHttpResponseHandler responseHandler)
          Perform a HTTP PUT request and track the Android Context which initiated the request.
 void put(android.content.Context context, String url, org.apache.http.HttpEntity entity, String contentType, AsyncHttpResponseHandler responseHandler)
          Perform a HTTP PUT request and track the Android Context which initiated the request.
 void put(android.content.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.
protected  void sendRequest(org.apache.http.impl.client.DefaultHttpClient client, org.apache.http.protocol.HttpContext httpContext, org.apache.http.client.methods.HttpUriRequest uriRequest, String contentType, AsyncHttpResponseHandler responseHandler, android.content.Context context)
           
 void setBasicAuth(String user, String pass)
          Sets basic authentication for the request.
 void setBasicAuth(String user, String pass, org.apache.http.auth.AuthScope scope)
          Sets basic authentication for the request.
 void setCookieStore(org.apache.http.client.CookieStore cookieStore)
          Sets an optional CookieStore to use when making requests
 void setSSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory sslSocketFactory)
          Sets the SSLSocketFactory to user when making requests.
 void setThreadPool(ThreadPoolExecutor threadPool)
          Overrides the threadpool implementation used when queuing/pooling requests.
 void setTimeout(int timeout)
          Sets the connection time oout.
 void setUserAgent(String userAgent)
          Sets the User-Agent header to be sent with each request.
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

构造方法详细信息

AsyncHttpClient

public AsyncHttpClient()
Creates a new AsyncHttpClient.

方法详细信息

getHttpClient

public org.apache.http.client.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.


getHttpContext

public org.apache.http.protocol.HttpContext getHttpContext()
Get the underlying HttpContext instance. This is useful for getting and setting fine-grained settings for requests by accessing the context's attributes such as the CookieStore.


setCookieStore

public void setCookieStore(org.apache.http.client.CookieStore cookieStore)
Sets an optional CookieStore to use when making requests

参数:
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.

参数:
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.

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

setTimeout

public void setTimeout(int timeout)
Sets the connection time oout. By default, 10 seconds

参数:
timeout - the connect/socket timeout in milliseconds

setSSLSocketFactory

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

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

addHeader

public void addHeader(String header,
                      String value)
Sets headers that will be added to all requests this client makes (before sending).

参数:
header - the name of the header
value - the contents of the header

setBasicAuth

public void setBasicAuth(String user,
                         String pass)
Sets basic authentication for the request. Uses AuthScope.ANY. This is the same as setBasicAuth('username','password',AuthScope.ANY)

参数:
username -
password -

setBasicAuth

public void setBasicAuth(String user,
                         String pass,
                         org.apache.http.auth.AuthScope scope)
Sets basic authentication for the request. You should pass in your AuthScope for security. It should be like this setBasicAuth("username","password", new AuthScope("host",port,AuthScope.ANY_REALM))

参数:
username -
password -
scope - - an AuthScope object

cancelRequests

public void cancelRequests(android.content.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.

参数:
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.

参数:
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.

参数:
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(android.content.Context context,
                String url,
                AsyncHttpResponseHandler responseHandler)
Perform a HTTP GET request without any parameters and track the Android Context which initiated the request.

参数:
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(android.content.Context context,
                String url,
                RequestParams params,
                AsyncHttpResponseHandler responseHandler)
Perform a HTTP GET request and track the Android Context which initiated the request.

参数:
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.

get

public void get(android.content.Context context,
                String url,
                org.apache.http.Header[] headers,
                RequestParams params,
                AsyncHttpResponseHandler responseHandler)
Perform a HTTP GET request and track the Android Context which initiated the request with customized headers

参数:
url - the URL to send the request to.
headers - set headers only for this request
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.

参数:
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.

参数:
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(android.content.Context context,
                 String url,
                 RequestParams params,
                 AsyncHttpResponseHandler responseHandler)
Perform a HTTP POST request and track the Android Context which initiated the request.

参数:
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(android.content.Context context,
                 String url,
                 org.apache.http.HttpEntity entity,
                 String contentType,
                 AsyncHttpResponseHandler responseHandler)
Perform a HTTP POST request and track the Android Context which initiated the request.

参数:
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.

post

public void post(android.content.Context context,
                 String url,
                 org.apache.http.Header[] headers,
                 RequestParams params,
                 String contentType,
                 AsyncHttpResponseHandler responseHandler)
Perform a HTTP POST request and track the Android Context which initiated the request. Set headers only for this request

参数:
context - the Android Context which initiated the request.
url - the URL to send the request to.
headers - set headers only for this request
params - additional POST parameters to send with the request.
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.

post

public void post(android.content.Context context,
                 String url,
                 org.apache.http.Header[] headers,
                 org.apache.http.HttpEntity entity,
                 String contentType,
                 AsyncHttpResponseHandler responseHandler)
Perform a HTTP POST request and track the Android Context which initiated the request. Set headers only for this request

参数:
context - the Android Context which initiated the request.
url - the URL to send the request to.
headers - set headers only for this request
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.

参数:
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.

参数:
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(android.content.Context context,
                String url,
                RequestParams params,
                AsyncHttpResponseHandler responseHandler)
Perform a HTTP PUT request and track the Android Context which initiated the request.

参数:
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(android.content.Context context,
                String url,
                org.apache.http.HttpEntity entity,
                String contentType,
                AsyncHttpResponseHandler responseHandler)
Perform a HTTP PUT request and track the Android Context which initiated the request. And set one-time headers for the request

参数:
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(android.content.Context context,
                String url,
                org.apache.http.Header[] headers,
                org.apache.http.HttpEntity entity,
                String contentType,
                AsyncHttpResponseHandler responseHandler)
Perform a HTTP PUT request and track the Android Context which initiated the request. And set one-time headers for the request

参数:
context - the Android Context which initiated the request.
url - the URL to send the request to.
headers - set one-time headers for this request
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.

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

delete

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

参数:
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.

delete

public void delete(android.content.Context context,
                   String url,
                   org.apache.http.Header[] headers,
                   AsyncHttpResponseHandler responseHandler)
Perform a HTTP DELETE request.

参数:
context - the Android Context which initiated the request.
url - the URL to send the request to.
headers - set one-time headers for this request
responseHandler - the response handler instance that should handle the response.

sendRequest

protected void sendRequest(org.apache.http.impl.client.DefaultHttpClient client,
                           org.apache.http.protocol.HttpContext httpContext,
                           org.apache.http.client.methods.HttpUriRequest uriRequest,
                           String contentType,
                           AsyncHttpResponseHandler responseHandler,
                           android.content.Context context)

getUrlWithQueryString

public static String getUrlWithQueryString(String url,
                                           RequestParams params)


Copyright © 2013. All Rights Reserved.