@@ -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,16 +146,23 @@ public void run() {
122
146
postRunnable (new Runnable () {
123
147
@ Override
124
148
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 ) {
126
153
onSuccess (statusCode , headers , (JSONObject ) jsonResponse );
127
154
} else if (jsonResponse instanceof JSONArray ) {
128
155
onSuccess (statusCode , headers , (JSONArray ) jsonResponse );
129
156
} 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
+ }
131
163
} else {
132
164
onFailure (statusCode , headers , new JSONException ("Unexpected response type " + jsonResponse .getClass ().getName ()), (JSONObject ) null );
133
165
}
134
-
135
166
}
136
167
});
137
168
} catch (final JSONException ex ) {
@@ -166,7 +197,10 @@ public void run() {
166
197
postRunnable (new Runnable () {
167
198
@ Override
168
199
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 ) {
170
204
onFailure (statusCode , headers , throwable , (JSONObject ) jsonResponse );
171
205
} else if (jsonResponse instanceof JSONArray ) {
172
206
onFailure (statusCode , headers , throwable , (JSONArray ) jsonResponse );
@@ -220,13 +254,36 @@ protected Object parseResponse(byte[] responseBody) throws JSONException {
220
254
if (jsonString .startsWith (UTF8_BOM )) {
221
255
jsonString = jsonString .substring (1 );
222
256
}
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
+ }
225
273
}
226
274
}
227
275
if (result == null ) {
228
276
result = jsonString ;
229
277
}
230
278
return result ;
231
279
}
280
+
281
+ public boolean isUseRFC5179CompatibilityMode () {
282
+ return useRFC5179CompatibilityMode ;
283
+ }
284
+
285
+ public void setUseRFC5179CompatibilityMode (boolean useRFC5179CompatibilityMode ) {
286
+ this .useRFC5179CompatibilityMode = useRFC5179CompatibilityMode ;
287
+ }
288
+
232
289
}
0 commit comments