public abstract class AsyncHttpResponseHandler extends java.lang.Object implements ResponseHandlerInterface
AsyncHttpClient. The
onSuccess(int, cz.msebera.android.httpclient.Header[], byte[]) method is designed to be anonymously
overridden with your own response handling code. Additionally, you can override the
onFailure(int, cz.msebera.android.httpclient.Header[], byte[], Throwable), onStart(), onFinish(), onRetry(int) and onProgress(long, long) methods as required.
For example:
AsyncHttpClient client = new AsyncHttpClient();
client.get("https://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(long bytesWritten, long totalSize) {
// Progress notification
}
@Override
public void onFinish() {
// Completed the request (either success or failure)
}
});
| Modifier and Type | Field and Description |
|---|---|
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 |
| Constructor and Description |
|---|
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.
|
| Modifier and Type | Method and Description |
|---|---|
java.lang.String |
getCharset() |
cz.msebera.android.httpclient.Header[] |
getRequestHeaders()
Returns Header[] which were used to request
|
java.net.URI |
getRequestURI()
Returns URI which was used to request
|
java.lang.Object |
getTag()
Will retrieve TAG Object if it's not already freed from memory
|
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,
cz.msebera.android.httpclient.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,
cz.msebera.android.httpclient.HttpResponse response)
This method is called once by the system when the request has been fully
sent, handled and finished.
|
void |
onPreProcessResponse(ResponseHandlerInterface instance,
cz.msebera.android.httpclient.HttpResponse response)
This method is called once by the system when the response is about to be
processed by the system.
|
void |
onProgress(long bytesWritten,
long 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,
cz.msebera.android.httpclient.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,
cz.msebera.android.httpclient.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(long bytesWritten,
long bytesTotal)
Notifies callback, that request (mainly uploading) has progressed
|
void |
sendResponseMessage(cz.msebera.android.httpclient.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,
cz.msebera.android.httpclient.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(cz.msebera.android.httpclient.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 |
setTag(java.lang.Object TAG)
Will set TAG to ResponseHandlerInterface implementation, which can be then obtained
in implemented methods, such as onSuccess, onFailure, ...
|
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
|
public static final java.lang.String DEFAULT_CHARSET
public static final java.lang.String UTF8_BOM
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 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.lang.Object getTag()
ResponseHandlerInterfacegetTag in interface ResponseHandlerInterfacepublic void setTag(java.lang.Object TAG)
ResponseHandlerInterfacesetTag in interface ResponseHandlerInterfaceTAG - Object to be set as TAG, will be placed in WeakReferencepublic java.net.URI getRequestURI()
ResponseHandlerInterfacegetRequestURI in interface ResponseHandlerInterfacepublic void setRequestURI(java.net.URI requestURI)
ResponseHandlerInterfacesetRequestURI in interface ResponseHandlerInterfacerequestURI - claimed request URIpublic cz.msebera.android.httpclient.Header[] getRequestHeaders()
ResponseHandlerInterfacegetRequestHeaders in interface ResponseHandlerInterfacepublic void setRequestHeaders(cz.msebera.android.httpclient.Header[] requestHeaders)
ResponseHandlerInterfacesetRequestHeaders in interface ResponseHandlerInterfacerequestHeaders - Headers, claimed to be from original requestpublic boolean getUseSynchronousMode()
ResponseHandlerInterfacegetUseSynchronousMode in interface ResponseHandlerInterfacepublic void setUseSynchronousMode(boolean sync)
ResponseHandlerInterfacesetUseSynchronousMode in interface ResponseHandlerInterfacesync - whether data should be handled on background Thread on UI Threadpublic boolean getUsePoolThread()
ResponseHandlerInterfacegetUsePoolThread in interface ResponseHandlerInterfacepublic void setUsePoolThread(boolean pool)
ResponseHandlerInterfacesetUsePoolThread in interface ResponseHandlerInterfacepool - if the ResponseHandler should run on pool's threadpublic java.lang.String getCharset()
public void setCharset(java.lang.String charset)
charset - to be used for the response string.public void onProgress(long bytesWritten,
long totalSize)
bytesWritten - offset from start of filetotalSize - total size of filepublic void onStart()
public void onFinish()
public void onPreProcessResponse(ResponseHandlerInterface instance, cz.msebera.android.httpclient.HttpResponse response)
ResponseHandlerInterfacePlease note: pre-processing does NOT run on the main thread, and thus any UI activities that you must perform should be properly dispatched to the app's UI thread.
onPreProcessResponse in interface ResponseHandlerInterfaceinstance - An instance of this response objectresponse - The response to pre-processedpublic void onPostProcessResponse(ResponseHandlerInterface instance, cz.msebera.android.httpclient.HttpResponse response)
ResponseHandlerInterfacePlease note: post-processing does NOT run on the main thread, and thus any UI activities that you must perform should be properly dispatched to the app's UI thread.
onPostProcessResponse in interface ResponseHandlerInterfaceinstance - An instance of this response objectresponse - The response to post-processpublic abstract void onSuccess(int statusCode,
cz.msebera.android.httpclient.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,
cz.msebera.android.httpclient.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(long bytesWritten,
long bytesTotal)
ResponseHandlerInterfacesendProgressMessage in interface ResponseHandlerInterfacebytesWritten - number of written bytesbytesTotal - number of total bytes to be writtenpublic final void sendSuccessMessage(int statusCode,
cz.msebera.android.httpclient.Header[] headers,
byte[] responseBytes)
ResponseHandlerInterfacesendSuccessMessage in interface ResponseHandlerInterfacestatusCode - HTTP status codeheaders - returned headersresponseBytes - returned datapublic final void sendFailureMessage(int statusCode,
cz.msebera.android.httpclient.Header[] headers,
byte[] responseBody,
java.lang.Throwable throwable)
ResponseHandlerInterfacesendFailureMessage in interface ResponseHandlerInterfacestatusCode - returned HTTP status codeheaders - returned headersresponseBody - returned datathrowable - cause of request failurepublic final void sendStartMessage()
ResponseHandlerInterfacesendStartMessage in interface ResponseHandlerInterfacepublic final void sendFinishMessage()
ResponseHandlerInterfacesendFinishMessage in interface ResponseHandlerInterfacepublic final void sendRetryMessage(int retryNo)
ResponseHandlerInterfacesendRetryMessage in interface ResponseHandlerInterfaceretryNo - number of retry within one requestpublic final void sendCancelMessage()
ResponseHandlerInterfacesendCancelMessage in interface 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(cz.msebera.android.httpclient.HttpResponse response)
throws java.io.IOException
ResponseHandlerInterfacesendResponseMessage in interface ResponseHandlerInterfaceresponse - HttpResponse object with datajava.io.IOException - if retrieving data from response fails