Skip to content

Commit 797b3b8

Browse files
author
Tom Whipple
committed
add JSON error content
1 parent c774527 commit 797b3b8

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,25 @@ protected void handleSuccessMessage(String responseBody) {
7878
protected Object parseResponse(String responseBody) throws JSONException {
7979
return new JSONTokener(responseBody).nextValue();
8080
}
81+
82+
/**
83+
* Handle cases where a failure is returned as JSON
84+
*/
85+
public void onFailure(Throwable e, JSONObject errorResponse) {}
86+
public void onFailure(Throwable e, JSONArray errorResponse) {}
87+
88+
@Override
89+
protected void handleFailureMessage(Throwable e, String responseBody) {
90+
super.handleFailureMessage(e, responseBody);
91+
try {
92+
Object jsonResponse = parseResponse(responseBody);
93+
if(jsonResponse instanceof JSONObject) {
94+
onFailure(e, (JSONObject)jsonResponse);
95+
} else if(jsonResponse instanceof JSONArray) {
96+
onFailure(e, (JSONArray)jsonResponse);
97+
}
98+
} catch(JSONException ex) {
99+
onFailure(e, responseBody);
100+
}
101+
}
81102
}

0 commit comments

Comments
 (0)