public abstract class AsyncHttpResponseHandler extends java.lang.Object implements ResponseHandlerInterface
AsyncHttpClient. The
{onSuccess(int, Header[], byte[])} method is designed to be anonymously
overridden with your own response handling code. Additionally, you can override the {onFailure(int, Header[], byte[], Throwable)}, {onStart()},
onFinish(), onRetry(int) and onProgress(int, int) methods as required.
For example:
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://www.google.com", new AsyncHttpResponseHandler() {
@Override
public void onStart() {
// Initiated the request
}
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
// Successfully got a response
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable
error)
{
// Response failed :(
}
@Override
public void onRetry(int retryNo) {
// Request was retried
}
@Override
public void onProgress(int bytesWritten, int totalSize) {
// Progress notification
}
@Override
public void onFinish() {
// Completed the request (either success or failure)
}
});
| 限定符和类型 | 字段和说明 |
|---|---|
protected static int |
BUFFER_SIZE |
protected static int |
CANCEL_MESSAGE |
static java.lang.String |
DEFAULT_CHARSET |
protected static int |
FAILURE_MESSAGE |
protected static int |
FINISH_MESSAGE |
protected static int |
PROGRESS_MESSAGE |
protected static int |
RETRY_MESSAGE |
protected static int |
START_MESSAGE |
protected static int |
SUCCESS_MESSAGE |
static java.lang.String |
UTF8_BOM |
| 构造器和说明 |
|---|
AsyncHttpResponseHandler()
Creates a new AsyncHttpResponseHandler
|
AsyncHttpResponseHandler(boolean usePoolThread)
Creates a new AsyncHttpResponseHandler and decide whether the callbacks
will be fired on current thread's looper or the pool thread's.
|
AsyncHttpResponseHandler(android.os.Looper looper)
Creates a new AsyncHttpResponseHandler with a user-supplied looper.
|
| 限定符和类型 | 方法和说明 |
|---|---|
java.lang.String |
getCharset() |
Header[] |
getRequestHeaders()
Returns Header[] which were used to request
|
java.net.URI |
getRequestURI()
Returns URI which was used to request
|
boolean |
getUsePoolThread()
Returns whether the handler should be executed on the pool's thread
or the UI thread
|
boolean |
getUseSynchronousMode()
Returns whether the handler is asynchronous or synchronous
|
protected void |
handleMessage(android.os.Message message) |
protected android.os.Message |
obtainMessage(int responseMessageId,
java.lang.Object responseMessageData)
Helper method to create Message instance from handler
|
void |
onCancel() |
abstract void |
onFailure(int statusCode,
Header[] headers,
byte[] responseBody,
java.lang.Throwable error)
Fired when a request fails to complete, override to handle in your own code
|
void |
onFinish()
Fired in all cases when the request is finished, after both success and failure, override to
handle in your own code
|
void |
onPostProcessResponse(ResponseHandlerInterface instance,
HttpResponse response)
This method is called once by the system when the request has been fully
sent, handled and finished.
|
void |
onPreProcessResponse(ResponseHandlerInterface instance,
HttpResponse response)
This method is called once by the system when the response is about to be
processed by the system.
|
void |
onProgress(int bytesWritten,
int totalSize)
Fired when the request progress, override to handle in your own code
|
void |
onRetry(int retryNo)
Fired when a retry occurs, override to handle in your own code
|
void |
onStart()
Fired when the request is started, override to handle in your own code
|
abstract void |
onSuccess(int statusCode,
Header[] headers,
byte[] responseBody)
Fired when a request returns successfully, override to handle in your own code
|
void |
onUserException(java.lang.Throwable error) |
protected void |
postRunnable(java.lang.Runnable runnable)
Helper method to send runnable into local handler loop
|
void |
sendCancelMessage()
Notifies callback, that request was cancelled
|
void |
sendFailureMessage(int statusCode,
Header[] headers,
byte[] responseBody,
java.lang.Throwable throwable)
Returns if request was completed with error code or failure of implementation
|
void |
sendFinishMessage()
Notifies callback, that request was completed and is being removed from thread pool
|
protected void |
sendMessage(android.os.Message msg) |
void |
sendProgressMessage(int bytesWritten,
int bytesTotal)
Notifies callback, that request (mainly uploading) has progressed
|
void |
sendResponseMessage(HttpResponse response)
Returns data whether request completed successfully
|
void |
sendRetryMessage(int retryNo)
Notifies callback of retrying request
|
void |
sendStartMessage()
Notifies callback, that request started execution
|
void |
sendSuccessMessage(int statusCode,
Header[] headers,
byte[] responseBytes)
Notifies callback, that request was handled successfully
|
void |
setCharset(java.lang.String charset)
Sets the charset for the response string.
|
void |
setRequestHeaders(Header[] requestHeaders)
Helper for handlers to receive Request Header[] info
|
void |
setRequestURI(java.net.URI requestURI)
Helper for handlers to receive Request URI info
|
void |
setUsePoolThread(boolean pool)
Sets whether the handler should be executed on the pool's thread or the
UI thread
|
void |
setUseSynchronousMode(boolean sync)
Can set, whether the handler should be asynchronous or synchronous
|
protected static final int SUCCESS_MESSAGE
protected static final int FAILURE_MESSAGE
protected static final int START_MESSAGE
protected static final int FINISH_MESSAGE
protected static final int PROGRESS_MESSAGE
protected static final int RETRY_MESSAGE
protected static final int CANCEL_MESSAGE
protected static final int BUFFER_SIZE
public static final java.lang.String DEFAULT_CHARSET
public static final java.lang.String UTF8_BOM
public AsyncHttpResponseHandler()
public AsyncHttpResponseHandler(android.os.Looper looper)
looper - The looper to work withpublic AsyncHttpResponseHandler(boolean usePoolThread)
usePoolThread - Whether to use the pool's thread to fire callbackspublic java.net.URI getRequestURI()
ResponseHandlerInterfacegetRequestURI 在接口中 ResponseHandlerInterfacepublic Header[] getRequestHeaders()
ResponseHandlerInterfacegetRequestHeaders 在接口中 ResponseHandlerInterfacepublic void setRequestURI(java.net.URI requestURI)
ResponseHandlerInterfacesetRequestURI 在接口中 ResponseHandlerInterfacerequestURI - claimed request URIpublic void setRequestHeaders(Header[] requestHeaders)
ResponseHandlerInterfacesetRequestHeaders 在接口中 ResponseHandlerInterfacerequestHeaders - Headers, claimed to be from original requestpublic boolean getUseSynchronousMode()
ResponseHandlerInterfacegetUseSynchronousMode 在接口中 ResponseHandlerInterfacepublic void setUseSynchronousMode(boolean sync)
ResponseHandlerInterfacesetUseSynchronousMode 在接口中 ResponseHandlerInterfacesync - whether data should be handled on background Thread on UI Threadpublic boolean getUsePoolThread()
ResponseHandlerInterfacegetUsePoolThread 在接口中 ResponseHandlerInterfacepublic void setUsePoolThread(boolean pool)
ResponseHandlerInterfacesetUsePoolThread 在接口中 ResponseHandlerInterfacepool - if the ResponseHandler should run on pool's threadpublic void setCharset(java.lang.String charset)
charset - to be used for the response string.public java.lang.String getCharset()
public void onProgress(int bytesWritten,
int totalSize)
bytesWritten - offset from start of filetotalSize - total size of filepublic void onStart()
public void onFinish()
public void onPreProcessResponse(ResponseHandlerInterface instance, HttpResponse response)
ResponseHandlerInterfaceonPreProcessResponse 在接口中 ResponseHandlerInterfaceinstance - An instance of this response objectresponse - The response to pre-processedpublic void onPostProcessResponse(ResponseHandlerInterface instance, HttpResponse response)
ResponseHandlerInterfaceonPostProcessResponse 在接口中 ResponseHandlerInterfaceinstance - An instance of this response objectresponse - The response to post-processpublic abstract void onSuccess(int statusCode,
Header[] headers,
byte[] responseBody)
statusCode - the status code of the responseheaders - return headers, if anyresponseBody - the body of the HTTP response from the serverpublic abstract void onFailure(int statusCode,
Header[] headers,
byte[] responseBody,
java.lang.Throwable error)
statusCode - return HTTP status codeheaders - return headers, if anyresponseBody - the response body, if anyerror - the underlying cause of the failurepublic void onRetry(int retryNo)
retryNo - number of retrypublic void onCancel()
public void onUserException(java.lang.Throwable error)
public final void sendProgressMessage(int bytesWritten,
int bytesTotal)
ResponseHandlerInterfacesendProgressMessage 在接口中 ResponseHandlerInterfacebytesWritten - number of written bytesbytesTotal - number of total bytes to be writtenpublic final void sendSuccessMessage(int statusCode,
Header[] headers,
byte[] responseBytes)
ResponseHandlerInterfacesendSuccessMessage 在接口中 ResponseHandlerInterfacestatusCode - HTTP status codeheaders - returned headersresponseBytes - returned datapublic final void sendFailureMessage(int statusCode,
Header[] headers,
byte[] responseBody,
java.lang.Throwable throwable)
ResponseHandlerInterfacesendFailureMessage 在接口中 ResponseHandlerInterfacestatusCode - returned HTTP status codeheaders - returned headersresponseBody - returned datathrowable - cause of request failurepublic final void sendStartMessage()
ResponseHandlerInterfacesendStartMessage 在接口中 ResponseHandlerInterfacepublic final void sendFinishMessage()
ResponseHandlerInterfacesendFinishMessage 在接口中 ResponseHandlerInterfacepublic final void sendRetryMessage(int retryNo)
ResponseHandlerInterfacesendRetryMessage 在接口中 ResponseHandlerInterfaceretryNo - number of retry within one requestpublic final void sendCancelMessage()
ResponseHandlerInterfacesendCancelMessage 在接口中 ResponseHandlerInterfaceprotected void handleMessage(android.os.Message message)
protected void sendMessage(android.os.Message msg)
protected void postRunnable(java.lang.Runnable runnable)
runnable - runnable instance, can be nullprotected android.os.Message obtainMessage(int responseMessageId,
java.lang.Object responseMessageData)
responseMessageId - constant to identify Handler messageresponseMessageData - object to be passed to message receiverpublic void sendResponseMessage(HttpResponse response)
throws java.io.IOException
ResponseHandlerInterfacesendResponseMessage 在接口中 ResponseHandlerInterfaceresponse - HttpResponse object with datajava.io.IOException - if retrieving data from response fails