4040 * {@link #onSuccess(int, org.apache.http.Header[], byte[])} method is designed to be anonymously
4141 * overridden with your own response handling code. <p> </p> Additionally, you can override the
4242 * {@link #onFailure(int, org.apache.http.Header[], byte[], Throwable)}, {@link #onStart()}, {@link
43- * #onFinish()}, {@link #onRetry()} and {@link #onProgress(int, int)} methods as required.
43+ * #onFinish()}, {@link #onRetry(int )} and {@link #onProgress(int, int)} methods as required.
4444 * <p> </p> For example: <p> </p>
4545 * <pre>
4646 * AsyncHttpClient client = new AsyncHttpClient();
@@ -119,8 +119,9 @@ public void setRequestHeaders(Header[] requestHeaders) {
119119 this .requestHeaders = requestHeaders ;
120120 }
121121
122- // avoid leaks by using a non-anonymous handler class
123- // with a weak reference
122+ /**
123+ * Avoid leaks by using a non-anonymous handler class with a weak reference
124+ */
124125 static class ResponderHandler extends Handler {
125126 private final WeakReference <AsyncHttpResponseHandler > mResponder ;
126127
@@ -137,8 +138,9 @@ public void handleMessage(Message msg) {
137138 }
138139 }
139140
141+ @ Override
140142 public boolean getUseSynchronousMode () {
141- return ( useSynchronousMode ) ;
143+ return useSynchronousMode ;
142144 }
143145
144146 @ Override
@@ -214,25 +216,23 @@ public void onFinish() {
214216
215217 /**
216218 * Fired when a retry occurs, override to handle in your own code
219+ *
220+ * @param retryNo number of retry
217221 */
218- public void onRetry () {
222+ public void onRetry (int retryNo ) {
223+ Log .d (LOG_TAG , String .format ("Request retry no. %d" , retryNo ));
219224 }
220225
221-
222- //
223- // Pre-processing of messages (executes in background threadpool thread)
224- //
225-
226226 final public void sendProgressMessage (int bytesWritten , int bytesTotal ) {
227227 sendMessage (obtainMessage (PROGRESS_MESSAGE , new Object []{bytesWritten , bytesTotal }));
228228 }
229229
230- final public void sendSuccessMessage (int statusCode , Header [] headers , byte [] responseBody ) {
231- sendMessage (obtainMessage (SUCCESS_MESSAGE , new Object []{statusCode , headers , responseBody }));
230+ final public void sendSuccessMessage (int statusCode , Header [] headers , byte [] responseBytes ) {
231+ sendMessage (obtainMessage (SUCCESS_MESSAGE , new Object []{statusCode , headers , responseBytes }));
232232 }
233233
234- final public void sendFailureMessage (int statusCode , Header [] headers , byte [] responseBody , Throwable error ) {
235- sendMessage (obtainMessage (FAILURE_MESSAGE , new Object []{statusCode , headers , responseBody , error }));
234+ final public void sendFailureMessage (int statusCode , Header [] headers , byte [] responseBody , Throwable throwable ) {
235+ sendMessage (obtainMessage (FAILURE_MESSAGE , new Object []{statusCode , headers , responseBody , throwable }));
236236 }
237237
238238 final public void sendStartMessage () {
@@ -243,25 +243,25 @@ final public void sendFinishMessage() {
243243 sendMessage (obtainMessage (FINISH_MESSAGE , null ));
244244 }
245245
246- final public void sendRetryMessage () {
247- sendMessage (obtainMessage (RETRY_MESSAGE , null ));
246+ final public void sendRetryMessage (int retryNo ) {
247+ sendMessage (obtainMessage (RETRY_MESSAGE , new Object []{ retryNo } ));
248248 }
249249
250250 // Methods which emulate android's Handler and Message methods
251- protected void handleMessage (Message msg ) {
251+ protected void handleMessage (Message message ) {
252252 Object [] response ;
253253
254- switch (msg .what ) {
254+ switch (message .what ) {
255255 case SUCCESS_MESSAGE :
256- response = (Object []) msg .obj ;
256+ response = (Object []) message .obj ;
257257 if (response != null && response .length >= 3 ) {
258258 onSuccess ((Integer ) response [0 ], (Header []) response [1 ], (byte []) response [2 ]);
259259 } else {
260260 Log .e (LOG_TAG , "SUCCESS_MESSAGE didn't got enough params" );
261261 }
262262 break ;
263263 case FAILURE_MESSAGE :
264- response = (Object []) msg .obj ;
264+ response = (Object []) message .obj ;
265265 if (response != null && response .length >= 4 ) {
266266 onFailure ((Integer ) response [0 ], (Header []) response [1 ], (byte []) response [2 ], (Throwable ) response [3 ]);
267267 } else {
@@ -275,7 +275,7 @@ protected void handleMessage(Message msg) {
275275 onFinish ();
276276 break ;
277277 case PROGRESS_MESSAGE :
278- response = (Object []) msg .obj ;
278+ response = (Object []) message .obj ;
279279 if (response != null && response .length >= 2 ) {
280280 try {
281281 onProgress ((Integer ) response [0 ], (Integer ) response [1 ]);
@@ -287,7 +287,11 @@ protected void handleMessage(Message msg) {
287287 }
288288 break ;
289289 case RETRY_MESSAGE :
290- onRetry ();
290+ response = (Object []) message .obj ;
291+ if (response != null && response .length == 1 )
292+ onRetry ((Integer ) response [0 ]);
293+ else
294+ Log .e (LOG_TAG , "RETRY_MESSAGE didn't get enough params" );
291295 break ;
292296 }
293297 }
@@ -300,21 +304,33 @@ protected void sendMessage(Message msg) {
300304 }
301305 }
302306
303- protected void postRunnable (Runnable r ) {
304- if (r != null ) {
305- handler .post (r );
307+ /**
308+ * Helper method to send runnable into local handler loop
309+ *
310+ * @param runnable runnable instance, can be null
311+ */
312+ protected void postRunnable (Runnable runnable ) {
313+ if (runnable != null ) {
314+ handler .post (runnable );
306315 }
307316 }
308317
309- protected Message obtainMessage (int responseMessage , Object response ) {
318+ /**
319+ * Helper method to create Message instance from handler
320+ *
321+ * @param responseMessageId constant to identify Handler message
322+ * @param responseMessageData object to be passed to message receiver
323+ * @return Message instance, should not be null
324+ */
325+ protected Message obtainMessage (int responseMessageId , Object responseMessageData ) {
310326 Message msg ;
311327 if (handler != null ) {
312- msg = handler .obtainMessage (responseMessage , response );
328+ msg = handler .obtainMessage (responseMessageId , responseMessageData );
313329 } else {
314330 msg = Message .obtain ();
315331 if (msg != null ) {
316- msg .what = responseMessage ;
317- msg .obj = response ;
332+ msg .what = responseMessageId ;
333+ msg .obj = responseMessageData ;
318334 }
319335 }
320336 return msg ;
@@ -338,6 +354,13 @@ public void sendResponseMessage(HttpResponse response) throws IOException {
338354 }
339355 }
340356
357+ /**
358+ * Returns byte array of response HttpEntity contents
359+ *
360+ * @param entity can be null
361+ * @return response entity body or null
362+ * @throws java.io.IOException if reading entity or creating byte array failed
363+ */
341364 byte [] getResponseData (HttpEntity entity ) throws IOException {
342365 byte [] responseBody = null ;
343366 if (entity != null ) {
0 commit comments