Skip to content

Commit 6472ec5

Browse files
committed
Merge pull request android-async-http#796 from NameX44/master
updated JsonHttpResponseHandler.java to handle string, numeric, boolean ...
2 parents 6857a93 + 840828c commit 6472ec5

File tree

2 files changed

+66
-9
lines changed

2 files changed

+66
-9
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: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public class JsonHttpResponseHandler extends TextHttpResponseHandler {
3939

4040
private static final String LOG_TAG = "JsonHttpResponseHandler";
4141

42+
43+
private boolean useRFC5179CompatibilityMode = true;
44+
4245
/**
4346
* Creates new JsonHttpResponseHandler, with JSON String encoding UTF-8
4447
*/
@@ -47,14 +50,35 @@ public JsonHttpResponseHandler() {
4750
}
4851

4952
/**
50-
* Creates new JsonHttpRespnseHandler with given JSON String encoding
53+
* Creates new JsonHttpResponseHandler with given JSON String encoding
5154
*
5255
* @param encoding String encoding to be used when parsing JSON
5356
*/
5457
public JsonHttpResponseHandler(String encoding) {
5558
super(encoding);
5659
}
5760

61+
/**
62+
* Creates new JsonHttpResponseHandler with JSON String encoding UTF-8 and given RFC5179CompatibilityMode
63+
*
64+
* @param useRFC5179CompatibilityMode Boolean mode to use RFC5179 or latest
65+
*/
66+
public JsonHttpResponseHandler(boolean useRFC5179CompatibilityMode) {
67+
super(DEFAULT_CHARSET);
68+
this.useRFC5179CompatibilityMode = useRFC5179CompatibilityMode;
69+
}
70+
71+
/**
72+
* Creates new JsonHttpResponseHandler with given JSON String encoding and RFC5179CompatibilityMode
73+
*
74+
* @param encoding String encoding to be used when parsing JSON
75+
* @param useRFC5179CompatibilityMode Boolean mode to use RFC5179 or latest
76+
*/
77+
public JsonHttpResponseHandler(String encoding, boolean useRFC5179CompatibilityMode) {
78+
super(encoding);
79+
this.useRFC5179CompatibilityMode = useRFC5179CompatibilityMode;
80+
}
81+
5882
/**
5983
* Returns when request succeeds
6084
*
@@ -122,16 +146,23 @@ public void run() {
122146
postRunnable(new Runnable() {
123147
@Override
124148
public void run() {
125-
if (jsonResponse instanceof JSONObject) {
149+
// In RFC5179 a null value is not a valid JSON
150+
if(!useRFC5179CompatibilityMode && jsonResponse == null){
151+
onSuccess(statusCode, headers, (String) jsonResponse);
152+
}else if (jsonResponse instanceof JSONObject) {
126153
onSuccess(statusCode, headers, (JSONObject) jsonResponse);
127154
} else if (jsonResponse instanceof JSONArray) {
128155
onSuccess(statusCode, headers, (JSONArray) jsonResponse);
129156
} else if (jsonResponse instanceof String) {
130-
onFailure(statusCode, headers, (String) jsonResponse, new JSONException("Response cannot be parsed as JSON data"));
157+
// In RFC5179 a simple string value is not a valid JSON
158+
if ( useRFC5179CompatibilityMode){
159+
onFailure(statusCode, headers, (String) jsonResponse, new JSONException("Response cannot be parsed as JSON data"));
160+
}else{
161+
onSuccess(statusCode, headers, (String) jsonResponse);
162+
}
131163
} else {
132164
onFailure(statusCode, headers, new JSONException("Unexpected response type " + jsonResponse.getClass().getName()), (JSONObject) null);
133165
}
134-
135166
}
136167
});
137168
} catch (final JSONException ex) {
@@ -166,7 +197,10 @@ public void run() {
166197
postRunnable(new Runnable() {
167198
@Override
168199
public void run() {
169-
if (jsonResponse instanceof JSONObject) {
200+
// In RFC5179 a null value is not a valid JSON
201+
if (!useRFC5179CompatibilityMode && jsonResponse == null){
202+
onFailure(statusCode, headers, (String) jsonResponse, throwable);
203+
}else if (jsonResponse instanceof JSONObject) {
170204
onFailure(statusCode, headers, throwable, (JSONObject) jsonResponse);
171205
} else if (jsonResponse instanceof JSONArray) {
172206
onFailure(statusCode, headers, throwable, (JSONArray) jsonResponse);
@@ -220,13 +254,36 @@ protected Object parseResponse(byte[] responseBody) throws JSONException {
220254
if (jsonString.startsWith(UTF8_BOM)) {
221255
jsonString = jsonString.substring(1);
222256
}
223-
if (jsonString.startsWith("{") || jsonString.startsWith("[")) {
224-
result = new JSONTokener(jsonString).nextValue();
257+
if ( useRFC5179CompatibilityMode){
258+
if (jsonString.startsWith("{") || jsonString.startsWith("[")) {
259+
result = new JSONTokener(jsonString).nextValue();
260+
}
261+
}else{
262+
// Check if the string is an JSONObject style {} or JSONArray style []
263+
// If not we consider this as a string
264+
if (( jsonString.startsWith("{") && jsonString.endsWith("}") )
265+
|| jsonString.startsWith("[") && jsonString.endsWith("]") ) {
266+
result = new JSONTokener(jsonString).nextValue();
267+
}
268+
// Check if this is a String "my String value" and remove quote
269+
// Other value type (numerical, boolean) should be without quote
270+
else if ( jsonString.startsWith("\"") && jsonString.endsWith("\"") ){
271+
result = jsonString.substring(1,jsonString.length()-1);
272+
}
225273
}
226274
}
227275
if (result == null) {
228276
result = jsonString;
229277
}
230278
return result;
231279
}
280+
281+
public boolean isUseRFC5179CompatibilityMode() {
282+
return useRFC5179CompatibilityMode;
283+
}
284+
285+
public void setUseRFC5179CompatibilityMode(boolean useRFC5179CompatibilityMode) {
286+
this.useRFC5179CompatibilityMode = useRFC5179CompatibilityMode;
287+
}
288+
232289
}

0 commit comments

Comments
 (0)