@@ -72,6 +72,7 @@ public class AsyncHttpResponseHandler {
72
72
protected static final int FAILURE_MESSAGE = 1 ;
73
73
protected static final int START_MESSAGE = 2 ;
74
74
protected static final int FINISH_MESSAGE = 3 ;
75
+ protected static final int PROGRESS_MESSAGE = 4 ;
75
76
76
77
private Handler handler ;
77
78
private String responseCharset = "UTF-8" ;
@@ -110,6 +111,15 @@ public void handleMessage(Message msg) {
110
111
// Callbacks to be overridden, typically anonymously
111
112
//
112
113
114
+ /**
115
+ * Fired when the request progress, override to handle in your own code
116
+ *
117
+ * @param bytesWritten offset from start of file
118
+ * @param totalSize total size of file
119
+ */
120
+ public void onProgress (int bytesWritten , int totalSize ) {
121
+ }
122
+
113
123
/**
114
124
* Fired when the request is started, override to handle in your own code
115
125
*/
@@ -202,6 +212,10 @@ public void onFailure(int statusCode, Header[] headers, Throwable error, String
202
212
// Pre-processing of messages (executes in background threadpool thread)
203
213
//
204
214
215
+ protected void sendProgressMessage (int bytesWritten , int totalSize ) {
216
+ sendMessage (obtainMessage (PROGRESS_MESSAGE , new Object []{bytesWritten , totalSize }));
217
+ }
218
+
205
219
protected void sendSuccessMessage (int statusCode , Header [] headers , String responseBody ) {
206
220
sendMessage (obtainMessage (SUCCESS_MESSAGE , new Object []{statusCode , headers , responseBody }));
207
221
}
@@ -265,6 +279,10 @@ protected void handleMessage(Message msg) {
265
279
case FINISH_MESSAGE :
266
280
onFinish ();
267
281
break ;
282
+ case PROGRESS_MESSAGE :
283
+ response = (Object []) msg .obj ;
284
+ onProgress ((Integer ) response [0 ], (Integer ) response [1 ]);
285
+ break ;
268
286
}
269
287
}
270
288
@@ -279,7 +297,7 @@ protected void sendMessage(Message msg) {
279
297
protected Message obtainMessage (int responseMessage , Object response ) {
280
298
Message msg ;
281
299
if (handler != null ) {
282
- msg = this . handler .obtainMessage (responseMessage , response );
300
+ msg = handler .obtainMessage (responseMessage , response );
283
301
} else {
284
302
msg = Message .obtain ();
285
303
if (msg != null ) {
@@ -292,6 +310,10 @@ protected Message obtainMessage(int responseMessage, Object response) {
292
310
293
311
// Interface to AsyncHttpRequest
294
312
protected void sendResponseMessage (HttpResponse response ) {
313
+ if (response == null ) {
314
+ sendFailureMessage (0 , null , new IllegalStateException ("No response" ), (String ) null );
315
+ return ;
316
+ }
295
317
StatusLine status = response .getStatusLine ();
296
318
String responseBody = null ;
297
319
try {
@@ -303,7 +325,7 @@ protected void sendResponseMessage(HttpResponse response) {
303
325
}
304
326
} catch (IOException e ) {
305
327
try {
306
- if (response != null && response .getEntity () != null )
328
+ if (response .getEntity () != null )
307
329
response .getEntity ().consumeContent ();
308
330
} catch (Throwable t ) {
309
331
t .printStackTrace ();
0 commit comments