Skip to content

Commit cd212a0

Browse files
committed
Merge pull request android-async-http#19 from scottanderson/response-bodies
Pass error response bodies through
2 parents 63e0a56 + 2c189b9 commit cd212a0

File tree

3 files changed

+35
-21
lines changed

3 files changed

+35
-21
lines changed

src/com/loopj/android/http/AsyncHttpRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void run() {
5555
} catch (IOException e) {
5656
if(responseHandler != null) {
5757
responseHandler.sendFinishMessage();
58-
responseHandler.sendFailureMessage(e);
58+
responseHandler.sendFailureMessage(e, null);
5959
}
6060
}
6161
}

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

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* {@link AsyncHttpClient}. The {@link #onSuccess(String)} method is
3737
* designed to be anonymously overridden with your own response handling code.
3838
* <p>
39-
* Additionally, you can override the {@link #onFailure(Throwable)},
39+
* Additionally, you can override the {@link #onFailure(Throwable, String)},
4040
* {@link #onStart()}, and {@link #onFinish()} methods as required.
4141
* <p>
4242
* For example:
@@ -55,7 +55,7 @@
5555
* }
5656
*
5757
* &#064;Override
58-
* public void onFailure(Throwable e) {
58+
* public void onFailure(Throwable e, String response) {
5959
* // Response failed :(
6060
* }
6161
*
@@ -112,9 +112,20 @@ public void onSuccess(String content) {}
112112
/**
113113
* Fired when a request fails to complete, override to handle in your own code
114114
* @param error the underlying cause of the failure
115+
* @deprecated use {@link #onFailure(Throwable, String)}
115116
*/
116117
public void onFailure(Throwable error) {}
117118

119+
/**
120+
* Fired when a request fails to complete, override to handle in your own code
121+
* @param error the underlying cause of the failure
122+
* @param content the response body, if any
123+
*/
124+
public void onFailure(Throwable error, String content) {
125+
// By default, call the deprecated onFailure(Throwable) for compatibility
126+
onFailure(error);
127+
}
128+
118129

119130
//
120131
// Pre-processing of messages (executes in background threadpool thread)
@@ -124,8 +135,8 @@ protected void sendSuccessMessage(String responseBody) {
124135
sendMessage(obtainMessage(SUCCESS_MESSAGE, responseBody));
125136
}
126137

127-
protected void sendFailureMessage(Throwable e) {
128-
sendMessage(obtainMessage(FAILURE_MESSAGE, e));
138+
protected void sendFailureMessage(Throwable e, String responseBody) {
139+
sendMessage(obtainMessage(FAILURE_MESSAGE, new Object[]{e, responseBody}));
129140
}
130141

131142
protected void sendStartMessage() {
@@ -145,8 +156,8 @@ protected void handleSuccessMessage(String responseBody) {
145156
onSuccess(responseBody);
146157
}
147158

148-
protected void handleFailureMessage(Throwable e) {
149-
onFailure(e);
159+
protected void handleFailureMessage(Throwable e, String responseBody) {
160+
onFailure(e, responseBody);
150161
}
151162

152163

@@ -158,7 +169,8 @@ protected void handleMessage(Message msg) {
158169
handleSuccessMessage((String)msg.obj);
159170
break;
160171
case FAILURE_MESSAGE:
161-
handleFailureMessage((Throwable)msg.obj);
172+
Object[] repsonse = (Object[])msg.obj;
173+
handleFailureMessage((Throwable)repsonse[0], (String)repsonse[1]);
162174
break;
163175
case START_MESSAGE:
164176
onStart();
@@ -193,20 +205,22 @@ protected Message obtainMessage(int responseMessage, Object response) {
193205
// Interface to AsyncHttpRequest
194206
void sendResponseMessage(HttpResponse response) {
195207
StatusLine status = response.getStatusLine();
208+
String responseBody = null;
209+
try {
210+
HttpEntity entity = null;
211+
HttpEntity temp = response.getEntity();
212+
if(temp != null) {
213+
entity = new BufferedHttpEntity(temp);
214+
}
215+
responseBody = EntityUtils.toString(entity);
216+
} catch(IOException e) {
217+
sendFailureMessage(e, null);
218+
}
219+
196220
if(status.getStatusCode() >= 300) {
197-
sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()));
221+
sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()), responseBody);
198222
} else {
199-
try {
200-
HttpEntity entity = null;
201-
HttpEntity temp = response.getEntity();
202-
if(temp != null) {
203-
entity = new BufferedHttpEntity(temp);
204-
}
205-
206-
sendSuccessMessage(EntityUtils.toString(entity));
207-
} catch(IOException e) {
208-
sendFailureMessage(e);
209-
}
223+
sendSuccessMessage(responseBody);
210224
}
211225
}
212226
}

src/com/loopj/android/http/JsonHttpResponseHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected void handleSuccessMessage(String responseBody) {
7171
onSuccess((JSONArray)jsonResponse);
7272
}
7373
} catch(JSONException e) {
74-
onFailure(e);
74+
onFailure(e, responseBody);
7575
}
7676
}
7777

0 commit comments

Comments
 (0)