Skip to content

Commit

Permalink
Manual merge, Closing #269, #118, Closing #10, Closing #127, #154
Browse files Browse the repository at this point in the history
  • Loading branch information
smarek committed Oct 14, 2013
1 parent 89e0028 commit 9f73dc7
Show file tree
Hide file tree
Showing 4 changed files with 303 additions and 167 deletions.
19 changes: 13 additions & 6 deletions library/src/com/loopj/android/http/AsyncHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ public void post(String url, RequestParams params, AsyncHttpResponseHandler resp
* @param responseHandler the response handler instance that should handle the response.
*/
public void post(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
post(context, url, paramsToEntity(params), null, responseHandler);
post(context, url, paramsToEntity(params, responseHandler), null, responseHandler);
}

/**
Expand Down Expand Up @@ -606,7 +606,7 @@ public void post(Context context, String url, HttpEntity entity, String contentT
public void post(Context context, String url, Header[] headers, RequestParams params, String contentType,
AsyncHttpResponseHandler responseHandler) {
HttpEntityEnclosingRequestBase request = new HttpPost(url);
if (params != null) request.setEntity(paramsToEntity(params));
if (params != null) request.setEntity(paramsToEntity(params, responseHandler));
if (headers != null) request.setHeaders(headers);
sendRequest(httpClient, httpContext, request, contentType,
responseHandler, context);
Expand Down Expand Up @@ -668,7 +668,7 @@ public void put(String url, RequestParams params, AsyncHttpResponseHandler respo
* @param responseHandler the response handler instance that should handle the response.
*/
public void put(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
put(context, url, paramsToEntity(params), null, responseHandler);
put(context, url, paramsToEntity(params, responseHandler), null, responseHandler);
}

/**
Expand Down Expand Up @@ -793,11 +793,18 @@ public static String getUrlWithQueryString(String url, RequestParams params) {
return url;
}

private HttpEntity paramsToEntity(RequestParams params) {
private HttpEntity paramsToEntity(RequestParams params, AsyncHttpResponseHandler responseHandler) {
HttpEntity entity = null;

if (params != null) {
entity = params.getEntity();
try {
if (params != null) {
entity = params.getEntity(responseHandler);
}
} catch (Throwable t) {
if (responseHandler != null)
responseHandler.sendFailureMessage(0, null, t, (String) null);
else
t.printStackTrace();
}

return entity;
Expand Down
26 changes: 24 additions & 2 deletions library/src/com/loopj/android/http/AsyncHttpResponseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public class AsyncHttpResponseHandler {
protected static final int FAILURE_MESSAGE = 1;
protected static final int START_MESSAGE = 2;
protected static final int FINISH_MESSAGE = 3;
protected static final int PROGRESS_MESSAGE = 4;

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

/**
* Fired when the request progress, override to handle in your own code
*
* @param bytesWritten offset from start of file
* @param totalSize total size of file
*/
public void onProgress(int bytesWritten, int totalSize) {
}

/**
* Fired when the request is started, override to handle in your own code
*/
Expand Down Expand Up @@ -202,6 +212,10 @@ public void onFailure(int statusCode, Header[] headers, Throwable error, String
// Pre-processing of messages (executes in background threadpool thread)
//

protected void sendProgressMessage(int bytesWritten, int totalSize) {
sendMessage(obtainMessage(PROGRESS_MESSAGE, new Object[]{bytesWritten, totalSize}));
}

protected void sendSuccessMessage(int statusCode, Header[] headers, String responseBody) {
sendMessage(obtainMessage(SUCCESS_MESSAGE, new Object[]{statusCode, headers, responseBody}));
}
Expand Down Expand Up @@ -265,6 +279,10 @@ protected void handleMessage(Message msg) {
case FINISH_MESSAGE:
onFinish();
break;
case PROGRESS_MESSAGE:
response = (Object[]) msg.obj;
onProgress((Integer) response[0], (Integer) response[1]);
break;
}
}

Expand All @@ -279,7 +297,7 @@ protected void sendMessage(Message msg) {
protected Message obtainMessage(int responseMessage, Object response) {
Message msg;
if (handler != null) {
msg = this.handler.obtainMessage(responseMessage, response);
msg = handler.obtainMessage(responseMessage, response);
} else {
msg = Message.obtain();
if (msg != null) {
Expand All @@ -292,6 +310,10 @@ protected Message obtainMessage(int responseMessage, Object response) {

// Interface to AsyncHttpRequest
protected void sendResponseMessage(HttpResponse response) {
if (response == null) {
sendFailureMessage(0, null, new IllegalStateException("No response"), (String) null);
return;
}
StatusLine status = response.getStatusLine();
String responseBody = null;
try {
Expand All @@ -303,7 +325,7 @@ protected void sendResponseMessage(HttpResponse response) {
}
} catch (IOException e) {
try {
if (response != null && response.getEntity() != null)
if (response.getEntity() != null)
response.getEntity().consumeContent();
} catch (Throwable t) {
t.printStackTrace();
Expand Down

0 comments on commit 9f73dc7

Please sign in to comment.