Skip to content

Commit c49fc5b

Browse files
committed
Changed JsonHttpResponseHandler.java to derive from TextHttpResponseHandler.java
1 parent daef5de commit c49fc5b

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

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

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,21 @@
4141
* Additionally, you can override the other event methods from the
4242
* parent class.
4343
*/
44-
public class JsonHttpResponseHandler extends AsyncHttpResponseHandler {
44+
public class JsonHttpResponseHandler extends TextHttpResponseHandler {
4545
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+
4659
//
4760
// Callbacks to be overridden, typically anonymously
4861
//
@@ -143,7 +156,7 @@ public void onFailure(int statusCode, Header[] headers, Throwable e, JSONArray e
143156
}
144157

145158
@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) {
147160
if (statusCode != HttpStatus.SC_NO_CONTENT) {
148161
new Thread(new Runnable() {
149162
@Override
@@ -181,7 +194,7 @@ public void run() {
181194
}
182195

183196
@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) {
185198
if (responseBody != null) {
186199
new Thread(new Runnable() {
187200
@Override
@@ -220,22 +233,17 @@ public void run() {
220233
}
221234
}
222235

223-
protected Object parseResponse(byte[] responseBody) throws JSONException {
236+
protected Object parseResponse(String responseBody) throws JSONException {
224237
if (null == responseBody)
225238
return null;
226239
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;
239247
}
240248
return result;
241249
}

library/src/com/loopj/android/http/TextHttpResponseHandler.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.loopj.android.http;
22

3+
import android.util.Log;
4+
35
import org.apache.http.Header;
46

57
import java.io.UnsupportedEncodingException;
@@ -40,6 +42,7 @@
4042
* </pre>
4143
*/
4244
public class TextHttpResponseHandler extends AsyncHttpResponseHandler {
45+
private static final String LOG_TAG = "TextHttpResponseHandler";
4346

4447
/**
4548
* Creates a new TextHttpResponseHandler
@@ -99,6 +102,7 @@ public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
99102
try {
100103
onSuccess(statusCode, headers, new String(responseBody, getCharset()));
101104
} catch (UnsupportedEncodingException e) {
105+
Log.v(LOG_TAG, "String encoding failed, calling onFailure(int, Header[], String, Throwable)");
102106
onFailure(0, headers, (String) null, e);
103107
}
104108
}
@@ -108,6 +112,7 @@ public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Thr
108112
try {
109113
onFailure(statusCode, headers, new String(responseBody, getCharset()), error);
110114
} catch (UnsupportedEncodingException e) {
115+
Log.v(LOG_TAG, "String encoding failed, calling onFailure(int, Header[], String, Throwable)");
111116
onFailure(0, headers, (String) null, e);
112117
}
113118
}

0 commit comments

Comments
 (0)