Skip to content

Commit 9f73dc7

Browse files
committed
1 parent 89e0028 commit 9f73dc7

File tree

4 files changed

+303
-167
lines changed

4 files changed

+303
-167
lines changed

library/src/com/loopj/android/http/AsyncHttpClient.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ public void post(String url, RequestParams params, AsyncHttpResponseHandler resp
574574
* @param responseHandler the response handler instance that should handle the response.
575575
*/
576576
public void post(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
577-
post(context, url, paramsToEntity(params), null, responseHandler);
577+
post(context, url, paramsToEntity(params, responseHandler), null, responseHandler);
578578
}
579579

580580
/**
@@ -606,7 +606,7 @@ public void post(Context context, String url, HttpEntity entity, String contentT
606606
public void post(Context context, String url, Header[] headers, RequestParams params, String contentType,
607607
AsyncHttpResponseHandler responseHandler) {
608608
HttpEntityEnclosingRequestBase request = new HttpPost(url);
609-
if (params != null) request.setEntity(paramsToEntity(params));
609+
if (params != null) request.setEntity(paramsToEntity(params, responseHandler));
610610
if (headers != null) request.setHeaders(headers);
611611
sendRequest(httpClient, httpContext, request, contentType,
612612
responseHandler, context);
@@ -668,7 +668,7 @@ public void put(String url, RequestParams params, AsyncHttpResponseHandler respo
668668
* @param responseHandler the response handler instance that should handle the response.
669669
*/
670670
public void put(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
671-
put(context, url, paramsToEntity(params), null, responseHandler);
671+
put(context, url, paramsToEntity(params, responseHandler), null, responseHandler);
672672
}
673673

674674
/**
@@ -793,11 +793,18 @@ public static String getUrlWithQueryString(String url, RequestParams params) {
793793
return url;
794794
}
795795

796-
private HttpEntity paramsToEntity(RequestParams params) {
796+
private HttpEntity paramsToEntity(RequestParams params, AsyncHttpResponseHandler responseHandler) {
797797
HttpEntity entity = null;
798798

799-
if (params != null) {
800-
entity = params.getEntity();
799+
try {
800+
if (params != null) {
801+
entity = params.getEntity(responseHandler);
802+
}
803+
} catch (Throwable t) {
804+
if (responseHandler != null)
805+
responseHandler.sendFailureMessage(0, null, t, (String) null);
806+
else
807+
t.printStackTrace();
801808
}
802809

803810
return entity;

library/src/com/loopj/android/http/AsyncHttpResponseHandler.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public class AsyncHttpResponseHandler {
7272
protected static final int FAILURE_MESSAGE = 1;
7373
protected static final int START_MESSAGE = 2;
7474
protected static final int FINISH_MESSAGE = 3;
75+
protected static final int PROGRESS_MESSAGE = 4;
7576

7677
private Handler handler;
7778
private String responseCharset = "UTF-8";
@@ -110,6 +111,15 @@ public void handleMessage(Message msg) {
110111
// Callbacks to be overridden, typically anonymously
111112
//
112113

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+
113123
/**
114124
* Fired when the request is started, override to handle in your own code
115125
*/
@@ -202,6 +212,10 @@ public void onFailure(int statusCode, Header[] headers, Throwable error, String
202212
// Pre-processing of messages (executes in background threadpool thread)
203213
//
204214

215+
protected void sendProgressMessage(int bytesWritten, int totalSize) {
216+
sendMessage(obtainMessage(PROGRESS_MESSAGE, new Object[]{bytesWritten, totalSize}));
217+
}
218+
205219
protected void sendSuccessMessage(int statusCode, Header[] headers, String responseBody) {
206220
sendMessage(obtainMessage(SUCCESS_MESSAGE, new Object[]{statusCode, headers, responseBody}));
207221
}
@@ -265,6 +279,10 @@ protected void handleMessage(Message msg) {
265279
case FINISH_MESSAGE:
266280
onFinish();
267281
break;
282+
case PROGRESS_MESSAGE:
283+
response = (Object[]) msg.obj;
284+
onProgress((Integer) response[0], (Integer) response[1]);
285+
break;
268286
}
269287
}
270288

@@ -279,7 +297,7 @@ protected void sendMessage(Message msg) {
279297
protected Message obtainMessage(int responseMessage, Object response) {
280298
Message msg;
281299
if (handler != null) {
282-
msg = this.handler.obtainMessage(responseMessage, response);
300+
msg = handler.obtainMessage(responseMessage, response);
283301
} else {
284302
msg = Message.obtain();
285303
if (msg != null) {
@@ -292,6 +310,10 @@ protected Message obtainMessage(int responseMessage, Object response) {
292310

293311
// Interface to AsyncHttpRequest
294312
protected void sendResponseMessage(HttpResponse response) {
313+
if (response == null) {
314+
sendFailureMessage(0, null, new IllegalStateException("No response"), (String) null);
315+
return;
316+
}
295317
StatusLine status = response.getStatusLine();
296318
String responseBody = null;
297319
try {
@@ -303,7 +325,7 @@ protected void sendResponseMessage(HttpResponse response) {
303325
}
304326
} catch (IOException e) {
305327
try {
306-
if (response != null && response.getEntity() != null)
328+
if (response.getEntity() != null)
307329
response.getEntity().consumeContent();
308330
} catch (Throwable t) {
309331
t.printStackTrace();

0 commit comments

Comments
 (0)