@@ -39,6 +39,9 @@ public class JsonHttpResponseHandler extends TextHttpResponseHandler {
39
39
40
40
private static final String LOG_TAG = "JsonHttpResponseHandler" ;
41
41
42
+
43
+ private boolean useRFC5179CompatibilityMode = true ;
44
+
42
45
/**
43
46
* Creates new JsonHttpResponseHandler, with JSON String encoding UTF-8
44
47
*/
@@ -47,14 +50,35 @@ public JsonHttpResponseHandler() {
47
50
}
48
51
49
52
/**
50
- * Creates new JsonHttpRespnseHandler with given JSON String encoding
53
+ * Creates new JsonHttpResponseHandler with given JSON String encoding
51
54
*
52
55
* @param encoding String encoding to be used when parsing JSON
53
56
*/
54
57
public JsonHttpResponseHandler (String encoding ) {
55
58
super (encoding );
56
59
}
57
60
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
+
58
82
/**
59
83
* Returns when request succeeds
60
84
*
@@ -122,14 +146,20 @@ public void run() {
122
146
postRunnable (new Runnable () {
123
147
@ Override
124
148
public void run () {
125
- if (jsonResponse == null ){
149
+ // In RFC5179 a null value is not a valid JSON
150
+ if (!useRFC5179CompatibilityMode && jsonResponse == null ){
126
151
onSuccess (statusCode , headers , (String ) jsonResponse );
127
152
}else if (jsonResponse instanceof JSONObject ) {
128
153
onSuccess (statusCode , headers , (JSONObject ) jsonResponse );
129
154
} else if (jsonResponse instanceof JSONArray ) {
130
155
onSuccess (statusCode , headers , (JSONArray ) jsonResponse );
131
156
} 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
+ }
133
163
} else {
134
164
onFailure (statusCode , headers , new JSONException ("Unexpected response type " + jsonResponse .getClass ().getName ()), (JSONObject ) null );
135
165
}
@@ -167,7 +197,8 @@ public void run() {
167
197
postRunnable (new Runnable () {
168
198
@ Override
169
199
public void run () {
170
- if (jsonResponse == null ){
200
+ // In RFC5179 a null value is not a valid JSON
201
+ if (!useRFC5179CompatibilityMode && jsonResponse == null ){
171
202
onFailure (statusCode , headers , (String ) jsonResponse , throwable );
172
203
}else if (jsonResponse instanceof JSONObject ) {
173
204
onFailure (statusCode , headers , throwable , (JSONObject ) jsonResponse );
@@ -223,20 +254,36 @@ protected Object parseResponse(byte[] responseBody) throws JSONException {
223
254
if (jsonString .startsWith (UTF8_BOM )) {
224
255
jsonString = jsonString .substring (1 );
225
256
}
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
+ }
238
273
}
239
274
}
240
- return jsonString ;
275
+ if (result == null ) {
276
+ result = jsonString ;
277
+ }
278
+ return result ;
279
+ }
280
+
281
+ public boolean isUseRFC5179CompatibilityMode () {
282
+ return useRFC5179CompatibilityMode ;
241
283
}
284
+
285
+ public void setUseRFC5179CompatibilityMode (boolean useRFC5179CompatibilityMode ) {
286
+ this .useRFC5179CompatibilityMode = useRFC5179CompatibilityMode ;
287
+ }
288
+
242
289
}
0 commit comments