Skip to content

Commit 840828c

Browse files
author
Felixledref
committed
updated JsonHttpResponseHandler.java with a boolean which handle RFC5179 or latest
1 parent 18eece1 commit 840828c

File tree

1 file changed

+64
-17
lines changed

1 file changed

+64
-17
lines changed

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

Lines changed: 64 additions & 17 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,14 +146,20 @@ public void run() {
122146
postRunnable(new Runnable() {
123147
@Override
124148
public void run() {
125-
if(jsonResponse == null){
149+
// In RFC5179 a null value is not a valid JSON
150+
if(!useRFC5179CompatibilityMode && jsonResponse == null){
126151
onSuccess(statusCode, headers, (String) jsonResponse);
127152
}else if (jsonResponse instanceof JSONObject) {
128153
onSuccess(statusCode, headers, (JSONObject) jsonResponse);
129154
} else if (jsonResponse instanceof JSONArray) {
130155
onSuccess(statusCode, headers, (JSONArray) jsonResponse);
131156
} else if (jsonResponse instanceof String) {
132-
onSuccess(statusCode, headers, (String) jsonResponse);
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+
}
133163
} else {
134164
onFailure(statusCode, headers, new JSONException("Unexpected response type " + jsonResponse.getClass().getName()), (JSONObject) null);
135165
}
@@ -167,7 +197,8 @@ public void run() {
167197
postRunnable(new Runnable() {
168198
@Override
169199
public void run() {
170-
if (jsonResponse == null){
200+
// In RFC5179 a null value is not a valid JSON
201+
if (!useRFC5179CompatibilityMode && jsonResponse == null){
171202
onFailure(statusCode, headers, (String) jsonResponse, throwable);
172203
}else if (jsonResponse instanceof JSONObject) {
173204
onFailure(statusCode, headers, throwable, (JSONObject) jsonResponse);
@@ -223,20 +254,36 @@ protected Object parseResponse(byte[] responseBody) throws JSONException {
223254
if (jsonString.startsWith(UTF8_BOM)) {
224255
jsonString = jsonString.substring(1);
225256
}
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("]") ) {
230-
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;
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+
}
238273
}
239274
}
240-
return jsonString;
275+
if (result == null) {
276+
result = jsonString;
277+
}
278+
return result;
279+
}
280+
281+
public boolean isUseRFC5179CompatibilityMode() {
282+
return useRFC5179CompatibilityMode;
241283
}
284+
285+
public void setUseRFC5179CompatibilityMode(boolean useRFC5179CompatibilityMode) {
286+
this.useRFC5179CompatibilityMode = useRFC5179CompatibilityMode;
287+
}
288+
242289
}

0 commit comments

Comments
 (0)