Skip to content

Commit 425b2d4

Browse files
committed
Fix NullPointerException in JsonHttpResponseHandler
If result is null after new JSONTokener(result) which means response is not a json type, assign result to responseBody to avoid makeing NullPointerException and also makes handleSuccessJsonMessage able to create a JSONException with right jsonResponse's class name.
1 parent eccbce2 commit 425b2d4

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,14 @@ protected void handleSuccessJsonMessage(Object jsonResponse) {
108108
protected Object parseResponse(String responseBody) throws JSONException {
109109
Object result = null;
110110
//trim the string to prevent start with blank, and test if the string is valid JSON, because the parser don't do this :(. If Json is not valid this will return null
111-
responseBody = responseBody.trim();
112-
if(responseBody.startsWith("{") || responseBody.startsWith("[")) {
113-
result = new JSONTokener(responseBody).nextValue();
114-
}
115-
return result;
111+
responseBody = responseBody.trim();
112+
if(responseBody.startsWith("{") || responseBody.startsWith("[")) {
113+
result = new JSONTokener(responseBody).nextValue();
114+
}
115+
if (result == null) {
116+
result = responseBody;
117+
}
118+
return result;
116119
}
117120

118121
@Override

0 commit comments

Comments
 (0)