转自:http://loopj.com/android-async-http/doc/com/loopj/android/http/AsyncHttpClient.html
需求是用户登录后才可以获取列表
后台通过session判断用户是否登录
android端需要保持session信息
直接代码
AsyncHttpCilentUtil工具类
参考:AsyncHttpClient的CookieStore问题
public class AsyncHttpClient extends java.lang.Object
RequestParams
instance, and responses can be handled by passing an anonymously overridden
ResponseHandlerInterface
instance.
For example:
AsyncHttpClient client = new AsyncHttpClient(); client.get("http://www.google.com", new ResponseHandlerInterface() { @Override public void onSuccess(String response) { System.out.println(response); } });
Constructor and Description |
---|
AsyncHttpClient()
Creates a new AsyncHttpClient with default constructor arguments values
|
AsyncHttpClient(boolean fixNoHttpResponseException, int httpPort, int httpsPort)
Creates new AsyncHttpClient using given params
|
AsyncHttpClient(int httpPort)
Creates a new AsyncHttpClient.
|
AsyncHttpClient(int httpPort, int httpsPort)
Creates a new AsyncHttpClient.
|
AsyncHttpClient(org.apache.http.conn.scheme.SchemeRegistry schemeRegistry)
Creates a new AsyncHttpClient.
|
Modifier and Type | Method and Description |
---|---|
void |
addHeader(java.lang.String header, java.lang.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 |
clearBasicAuth()
Removes set basic auth credentials
|
RequestHandle |
delete(android.content.Context context, java.lang.String url, org.apache.http.Header[] headers, RequestParams params,ResponseHandlerInterface responseHandler)
Perform a HTTP DELETE request.
|
RequestHandle |
delete(android.content.Context context, java.lang.String url, org.apache.http.Header[] headers, ResponseHandlerInterface responseHandler)
Perform a HTTP DELETE request.
|
RequestHandle |
delete(android.content.Context context, java.lang.String url, ResponseHandlerInterface responseHandler)
Perform a HTTP DELETE request.
|
RequestHandle |
delete(java.lang.String url, ResponseHandlerInterface responseHandler)
Perform a HTTP DELETE request.
|
RequestHandle |
get(android.content.Context context, java.lang.String url, org.apache.http.Header[] headers, RequestParams params,ResponseHandlerInterface responseHandler)
Perform a HTTP GET request and track the Android Context which initiated the request with customized headers
|
RequestHandle |
get(android.content.Context context, java.lang.String url, RequestParams params, ResponseHandlerInterface responseHandler)
Perform a HTTP GET request and track the Android Context which initiated the request.
|
RequestHandle |
get(android.content.Context context, java.lang.String url, ResponseHandlerInterface responseHandler)
Perform a HTTP GET request without any parameters and track the Android Context which initiated the request.
|
RequestHandle |
get(java.lang.String url, RequestParams params, ResponseHandlerInterface responseHandler)
Perform a HTTP GET request with parameters.
|
RequestHandle |
get(java.lang.String url, ResponseHandlerInterface responseHandler)
Perform a HTTP GET request, without any parameters.
|
org.apache.http.client.HttpClient |
getHttpClient()
Get the underlying HttpClient instance.
|
org.apache.http.protocol.HttpContext |
getHttpContext()
Get the underlying HttpContext instance.
|
int |
getMaxConnections()
Returns current limit of parallel connections
|
int |
getTimeout()
Returns current socket timeout limit (milliseconds), default is 10000 (10sec)
|
static java.lang.String |
getUrlWithQueryString(boolean shouldEncodeUrl, java.lang.String url, RequestParams params)
Will encode url, if not disabled, and adds params on the end of it
|
RequestHandle |
head(android.content.Context context, java.lang.String url, org.apache.http.Header[] headers, RequestParams params,ResponseHandlerInterface responseHandler)
Perform a HTTP HEAD request and track the Android Context which initiated the request with customized headers
|
RequestHandle |
head(android.content.Context context, java.lang.String url, RequestParams params, ResponseHandlerInterface responseHandler)
Perform a HTTP HEAD request and track the Android Context which initiated the request.
|
RequestHandle |
head(android.content.Context context, java.lang.String url, ResponseHandlerInterface responseHandler)
Perform a HTTP HEAD request without any parameters and track the Android Context which initiated the request.
|
RequestHandle |
head(java.lang.String url, RequestParams params, ResponseHandlerInterface responseHandler)
Perform a HTTP HEAD request with parameters.
|
RequestHandle |
head(java.lang.String url, ResponseHandlerInterface responseHandler)
Perform a HTTP HEAD request, without any parameters.
|
boolean |
isUrlEncodingEnabled() |
RequestHandle |
post(android.content.Context context, java.lang.String url, org.apache.http.Header[] headers, org.apache.http.HttpEntity entity, java.lang.String contentType, ResponseHandlerInterface responseHandler)
Perform a HTTP POST request and track the Android Context which initiated the request.
|
RequestHandle |
post(android.content.Context context, java.lang.String url, org.apache.http.Header[] headers, RequestParams params, java.lang.String contentType,ResponseHandlerInterface responseHandler)
Perform a HTTP POST request and track the Android Context which initiated the request.
|
RequestHandle |
post(android.content.Context context, java.lang.String url, org.apache.http.HttpEntity entity, java.lang.String contentType,ResponseHandlerInterface responseHandler)
Perform a HTTP POST request and track the Android Context which initiated the request.
|
RequestHandle |
post(android.content.Context context, java.lang.String url, RequestParams params, ResponseHandlerInterface responseHandler)
Perform a HTTP POST request and track the Android Context which initiated the request.
|
RequestHandle |
post(java.lang.String url, RequestParams params, ResponseHandlerInterface responseHandler)
Perform a HTTP POST request with parameters.
|
RequestHandle |
post(java.lang.String url, ResponseHandlerInterface responseHandler)
Perform a HTTP POST request, without any parameters.
|
RequestHandle |
put(android.content.Context context, java.lang.String url, org.apache.http.Header[] headers, org.apache.http.HttpEntity entity, java.lang.String contentType,ResponseHandlerInterface responseHandler)
Perform a HTTP PUT request and track the Android Context which initiated the request.
|
RequestHandle |
put(android.content.Context context, java.lang.String url, org.apache.http.HttpEntity entity, java.lang.String contentType,ResponseHandlerInterface responseHandler)
Perform a HTTP PUT request and track the Android Context which initiated the request.
|
RequestHandle |
put(android.content.Context context, java.lang.String url, RequestParams params, ResponseHandlerInterface responseHandler)
Perform a HTTP PUT request and track the Android Context which initiated the request.
|
RequestHandle |
put(java.lang.String url, RequestParams params, ResponseHandlerInterface responseHandler)
Perform a HTTP PUT request with parameters.
|
RequestHandle |
put(java.lang.String url, ResponseHandlerInterface responseHandler)
Perform a HTTP PUT request, without any parameters.
|
void |
removeHeader(java.lang.String header)
Remove header from all requests this client makes (before sending).
|
protected RequestHandle |
sendRequest(org.apache.http.impl.client.DefaultHttpClient client, org.apache.http.protocol.HttpContext httpContext, org.apache.http.client.methods.HttpUriRequest uriRequest, java.lang.String contentType, ResponseHandlerInterface responseHandler, android.content.Context context)
Puts a new request in queue as a new thread in pool to be executed
|
void |
setBasicAuth(java.lang.String username, java.lang.String password)
Sets basic authentication for the request.
|
void |
setBasicAuth(java.lang.String username, java.lang.String password, 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 |
setEnableRedirects(boolean enableRedirects)
Simple interface method, to enable or disable redirects.
|
void |
setMaxConnections(int maxConnections)
Sets maximum limit of parallel connections
|
void |
setMaxRetriesAndTimeout(int retries, int timeout)
Sets the maximum number of retries and timeout for a particular Request.
|
void |
setProxy(java.lang.String hostname, int port)
Sets the Proxy by it's hostname and port
|
void |
setProxy(java.lang.String hostname, int port, java.lang.String username, java.lang.String password)
Sets the Proxy by it's hostname,port,username and password
|
void |
setSSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory sslSocketFactory)
Sets the SSLSocketFactory to user when making requests.
|
void |
setThreadPool(java.util.concurrent.ThreadPoolExecutor threadPool)
Overrides the threadpool implementation used when queuing/pooling requests.
|
void |
setTimeout(int timeout)
Set the connection and socket timeout.
|
void |
setURLEncodingEnabled(boolean enabled)
Sets state of URL encoding feature, see bug #227, this method allows you to turn off and on this auto-magic feature on-demand.
|
void |
setUserAgent(java.lang.String userAgent)
Sets the User-Agent header to be sent with each request.
|
public AsyncHttpClient()
public AsyncHttpClient(int httpPort)
httpPort
- non-standard HTTP-only port
public AsyncHttpClient(int httpPort, int httpsPort)
httpPort
- non-standard HTTP-only port
httpsPort
- non-standard HTTPS-only port
public AsyncHttpClient(boolean fixNoHttpResponseException, int httpPort, int httpsPort)
fixNoHttpResponseException
- Whether to fix or not issue, by ommiting SSL verification
httpPort
- HTTP port to be used, must be greater than 0
httpsPort
- HTTPS port to be used, must be greater than 0
public AsyncHttpClient(org.apache.http.conn.scheme.SchemeRegistry schemeRegistry)
schemeRegistry
- SchemeRegistry to be used
public org.apache.http.client.HttpClient getHttpClient()
public org.apache.http.protocol.HttpContext getHttpContext()
public void setCookieStore(org.apache.http.client.CookieStore cookieStore)
cookieStore
- The CookieStore implementation to use, usually an instance of
PersistentCookieStore
public void setThreadPool(java.util.concurrent.ThreadPoolExecutor threadPool)
threadPool
- an instance of
ThreadPoolExecutor
to use for queuing/pooling requests.
public void setEnableRedirects(boolean enableRedirects)
enableRedirects
- boolean
public void setUserAgent(java.lang.String userAgent)
userAgent
- the string to use in the User-Agent header.
public int getMaxConnections()
public void setMaxConnections(int maxConnections)
maxConnections
- maximum parallel connections, must be at least 1
public int getTimeout()
public void setTimeout(int timeout)
timeout
- the connect/socket timeout in milliseconds, at least 1 second
public void setProxy(java.lang.String hostname, int port)
hostname
- the hostname (IP or DNS name)
port
- the port number. -1 indicates the scheme default port.
public void setProxy(java.lang.String hostname, int port, java.lang.String username, java.lang.String password)
hostname
- the hostname (IP or DNS name)
port
- the port number. -1 indicates the scheme default port.
username
- the username
password
- the password
public void setSSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory sslSocketFactory)
sslSocketFactory
- the socket factory to use for https requests.
public void setMaxRetriesAndTimeout(int retries, int timeout)
retries
- maximum number of retries per request
timeout
- sleep between retries in milliseconds
public void addHeader(java.lang.String header, java.lang.String value)
header
- the name of the header
value
- the contents of the header
public void removeHeader(java.lang.String header)
header
- the name of the header
public void setBasicAuth(java.lang.String username, java.lang.String password)
username
- Basic Auth username
password
- Basic Auth password
public void setBasicAuth(java.lang.String username, java.lang.String password, org.apache.http.auth.AuthScope scope)
username
- Basic Auth username
password
- Basic Auth password
scope
- - an AuthScope object
public void clearBasicAuth()
public void cancelRequests(android.content.Context context, boolean mayInterruptIfRunning)
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.
public RequestHandle head(java.lang.String url, ResponseHandlerInterface responseHandler)
url
- the URL to send the request to.
responseHandler
- the response handler instance that should handle the response.
public RequestHandle head(java.lang.String url, RequestParams params, ResponseHandlerInterface responseHandler)
url
- the URL to send the request to.
params
- additional HEAD parameters to send with the request.
responseHandler
- the response handler instance that should handle the response.
public RequestHandle head(android.content.Context context, java.lang.String url, ResponseHandlerInterface responseHandler)
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.
public RequestHandle head(android.content.Context context, java.lang.String url, RequestParams params, ResponseHandlerInterface responseHandler)
context
- the Android Context which initiated the request.
url
- the URL to send the request to.
params
- additional HEAD parameters to send with the request.
responseHandler
- the response handler instance that should handle the response.
public RequestHandle head(android.content.Context context, java.lang.String url, org.apache.http.Header[] headers, RequestParams params, ResponseHandlerInterface responseHandler)
context
- Context to execute request against
url
- the URL to send the request to.
headers
- set headers only for this request
params
- additional HEAD parameters to send with the request.
responseHandler
- the response handler instance that should handle the response.
public RequestHandle get(java.lang.String url, ResponseHandlerInterface responseHandler)
url
- the URL to send the request to.
responseHandler
- the response handler instance that should handle the response.
public RequestHandle get(java.lang.String url, RequestParams params, ResponseHandlerInterface responseHandler)
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.
public RequestHandle get(android.content.Context context, java.lang.String url, ResponseHandlerInterface responseHandler)
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.
public RequestHandle get(android.content.Context context, java.lang.String url, RequestParams params, ResponseHandlerInterface responseHandler)
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.
public RequestHandle get(android.content.Context context, java.lang.String url, org.apache.http.Header[] headers, RequestParams params, ResponseHandlerInterface responseHandler)
context
- Context to execute request against
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.
public RequestHandle post(java.lang.String url, ResponseHandlerInterface responseHandler)
url
- the URL to send the request to.
responseHandler
- the response handler instance that should handle the response.
public RequestHandle post(java.lang.String url, RequestParams params, ResponseHandlerInterface responseHandler)
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.
public RequestHandle post(android.content.Context context, java.lang.String url, RequestParams params, ResponseHandlerInterface responseHandler)
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.
public RequestHandle post(android.content.Context context, java.lang.String url, org.apache.http.HttpEntity entity, java.lang.String contentType, ResponseHandlerInterface responseHandler)
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 ha ndler instance that should handle the response.
public RequestHandle post(android.content.Context context, java.lang.String url, org.apache.http.Header[] headers, RequestParams params, java.lang.String contentType, ResponseHandlerInterface responseHandler)
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.
public RequestHandle post(android.content.Context context, java.lang.String url, org.apache.http.Header[] headers, org.apache.http.HttpEntity entity, java.lang.String contentType, ResponseHandlerInterface responseHandler)
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.
public RequestHandle put(java.lang.String url, ResponseHandlerInterface responseHandler)
url
- the URL to send the request to.
responseHandler
- the response handler instance that should handle the response.
public RequestHandle put(java.lang.String url, RequestParams params, ResponseHandlerInterface responseHandler)
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.
public RequestHandle put(android.content.Context context, java.lang.String url, RequestParams params, ResponseHandlerInterface responseHandler)
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.
public RequestHandle put(android.content.Context context, java.lang.String url, org.apache.http.HttpEntity entity, java.lang.String contentType, ResponseHandlerInterface responseHandler)
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.
public RequestHandle put(android.content.Context context, java.lang.String url, org.apache.http.Header[] headers, org.apache.http.HttpEntity entity, java.lang.String contentType, ResponseHandlerInterface responseHandler)
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.
public RequestHandle delete(java.lang.String url, ResponseHandlerInterface responseHandler)
url
- the URL to send the request to.
responseHandler
- the response handler instance that should handle the response.
public RequestHandle delete(android.content.Context context, java.lang.String url, ResponseHandlerInterface responseHandler)
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.
public RequestHandle delete(android.content.Context context, java.lang.String url, org.apache.http.Header[] headers, ResponseHandlerInterface responseHandler)
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.
public RequestHandle delete(android.content.Context context, java.lang.String url, org.apache.http.Header[] headers, RequestParams params, ResponseHandlerInterface responseHandler)
context
- the Android Context which initiated the request.
url
- the URL to send the request to.
headers
- set one-time headers for this request
params
- additional DELETE parameters or files to send along with request
responseHandler
- the response handler instance that should handle the response.
protected RequestHandle sendRequest(org.apache.http.impl.client.DefaultHttpClient client, org.apache.http.protocol.HttpContext httpContext, org.apache.http.client.methods.HttpUriRequest uriRequest, java.lang.String contentType, ResponseHandlerInterface responseHandler, android.content.Context context)
client
- HttpClient to be used for request, can differ in single requests
contentType
- MIME body type, for POST and PUT requests, may be null
context
- Context of Android application, to hold the reference of request
httpContext
- HttpContext in which the request will be executed
responseHandler
- ResponseHandler or its subclass to put the response into
uriRequest
- instance of HttpUriRequest, which means it must be of HttpDelete, HttpPost, HttpGet, HttpPut, etc.
public void setURLEncodingEnabled(boolean enabled)
enabled
- desired state of feature
public static java.lang.String getUrlWithQueryString(boolean shouldEncodeUrl, java.lang.String url, RequestParams params)
url
- String with URL, should be valid URL without params
params
- RequestParams to be appended on the end of URL
shouldEncodeUrl
- whether url should be encoded (replaces spaces with %20)
public boolean isUrlEncodingEnabled()