|
41 | 41 | * Additionally, you can override the other event methods from the
|
42 | 42 | * parent class.
|
43 | 43 | */
|
44 |
| -public class JsonHttpResponseHandler extends AsyncHttpResponseHandler { |
| 44 | +public class JsonHttpResponseHandler extends TextHttpResponseHandler { |
45 | 45 | private static final String LOG_TAG = "JsonHttpResponseHandler";
|
| 46 | + |
| 47 | + /** |
| 48 | + * Creates a new TextHttpResponseHandler |
| 49 | + */ |
| 50 | + |
| 51 | + public JsonHttpResponseHandler() { |
| 52 | + super(DEFAULT_CHARSET); |
| 53 | + } |
| 54 | + |
| 55 | + public JsonHttpResponseHandler(String encoding) { |
| 56 | + super(encoding); |
| 57 | + } |
| 58 | + |
46 | 59 | //
|
47 | 60 | // Callbacks to be overridden, typically anonymously
|
48 | 61 | //
|
@@ -143,7 +156,7 @@ public void onFailure(int statusCode, Header[] headers, Throwable e, JSONArray e
|
143 | 156 | }
|
144 | 157 |
|
145 | 158 | @Override
|
146 |
| - public void onSuccess(final int statusCode, final Header[] headers, final byte[] responseBody) { |
| 159 | + public void onSuccess(final int statusCode, final Header[] headers, final String responseBody) { |
147 | 160 | if (statusCode != HttpStatus.SC_NO_CONTENT) {
|
148 | 161 | new Thread(new Runnable() {
|
149 | 162 | @Override
|
@@ -181,7 +194,7 @@ public void run() {
|
181 | 194 | }
|
182 | 195 |
|
183 | 196 | @Override
|
184 |
| - public void onFailure(final int statusCode, final Header[] headers, final byte[] responseBody, final Throwable e) { |
| 197 | + public void onFailure(final int statusCode, final Header[] headers, final String responseBody, final Throwable e) { |
185 | 198 | if (responseBody != null) {
|
186 | 199 | new Thread(new Runnable() {
|
187 | 200 | @Override
|
@@ -220,22 +233,17 @@ public void run() {
|
220 | 233 | }
|
221 | 234 | }
|
222 | 235 |
|
223 |
| - protected Object parseResponse(byte[] responseBody) throws JSONException { |
| 236 | + protected Object parseResponse(String responseBody) throws JSONException { |
224 | 237 | if (null == responseBody)
|
225 | 238 | return null;
|
226 | 239 | Object result = null;
|
227 |
| - try { |
228 |
| - //trim the string to prevent start with blank, and test if the string is valid JSON, because the parser don't do this :(. If Json is not valid this will return null |
229 |
| - String jsonString = new String(responseBody, "UTF-8").trim(); |
230 |
| - if (jsonString.startsWith("{") || jsonString.startsWith("[")) { |
231 |
| - result = new JSONTokener(jsonString).nextValue(); |
232 |
| - } |
233 |
| - if (result == null) { |
234 |
| - result = jsonString; |
235 |
| - } |
236 |
| - } catch (UnsupportedEncodingException ex) { |
237 |
| - Log.v(LOG_TAG, "JSON parsing failed, calling onFailure(Throwable, JSONObject)"); |
238 |
| - onFailure(ex, (JSONObject) null); |
| 240 | + //trim the string to prevent start with blank, and test if the string is valid JSON, because the parser don't do this :(. If Json is not valid this will return null |
| 241 | + String jsonString = responseBody.trim(); |
| 242 | + if (jsonString.startsWith("{") || jsonString.startsWith("[")) { |
| 243 | + result = new JSONTokener(jsonString).nextValue(); |
| 244 | + } |
| 245 | + if (result == null) { |
| 246 | + result = jsonString; |
239 | 247 | }
|
240 | 248 | return result;
|
241 | 249 | }
|
|
0 commit comments