Skip to content

Commit 18eece1

Browse files
author
Felixledref
committed
updated JsonHttpResponseHandler.java to handle string, numeric, boolean and null value
1 parent 6857a93 commit 18eece1

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Wed Jul 02 18:01:59 CEST 2014
1+
#Thu Jan 22 17:47:44 CET 2015
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2-bin.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,17 @@ public void run() {
122122
postRunnable(new Runnable() {
123123
@Override
124124
public void run() {
125-
if (jsonResponse instanceof JSONObject) {
125+
if(jsonResponse == null){
126+
onSuccess(statusCode, headers, (String) jsonResponse);
127+
}else if (jsonResponse instanceof JSONObject) {
126128
onSuccess(statusCode, headers, (JSONObject) jsonResponse);
127129
} else if (jsonResponse instanceof JSONArray) {
128130
onSuccess(statusCode, headers, (JSONArray) jsonResponse);
129131
} else if (jsonResponse instanceof String) {
130-
onFailure(statusCode, headers, (String) jsonResponse, new JSONException("Response cannot be parsed as JSON data"));
132+
onSuccess(statusCode, headers, (String) jsonResponse);
131133
} else {
132134
onFailure(statusCode, headers, new JSONException("Unexpected response type " + jsonResponse.getClass().getName()), (JSONObject) null);
133135
}
134-
135136
}
136137
});
137138
} catch (final JSONException ex) {
@@ -166,7 +167,9 @@ public void run() {
166167
postRunnable(new Runnable() {
167168
@Override
168169
public void run() {
169-
if (jsonResponse instanceof JSONObject) {
170+
if (jsonResponse == null){
171+
onFailure(statusCode, headers, (String) jsonResponse, throwable);
172+
}else if (jsonResponse instanceof JSONObject) {
170173
onFailure(statusCode, headers, throwable, (JSONObject) jsonResponse);
171174
} else if (jsonResponse instanceof JSONArray) {
172175
onFailure(statusCode, headers, throwable, (JSONArray) jsonResponse);
@@ -220,13 +223,20 @@ protected Object parseResponse(byte[] responseBody) throws JSONException {
220223
if (jsonString.startsWith(UTF8_BOM)) {
221224
jsonString = jsonString.substring(1);
222225
}
223-
if (jsonString.startsWith("{") || jsonString.startsWith("[")) {
226+
// Check if the string is an JSONObject style {} or JSONArray style []
227+
// If not we consider this as a string
228+
if (( jsonString.startsWith("{") && jsonString.endsWith("}") )
229+
|| jsonString.startsWith("[") && jsonString.endsWith("]") ) {
224230
result = new JSONTokener(jsonString).nextValue();
231+
return result;
232+
}
233+
// Check if this is a String "my String value" and remove quote
234+
// Other value type (numerical, boolean) should be without quote
235+
else if ( jsonString.startsWith("\"") && jsonString.endsWith("\"") ){
236+
result = jsonString.substring(1,jsonString.length()-1);
237+
return result;
225238
}
226239
}
227-
if (result == null) {
228-
result = jsonString;
229-
}
230-
return result;
240+
return jsonString;
231241
}
232242
}

0 commit comments

Comments
 (0)