Skip to content

Commit 447976b

Browse files
committed
Removing UTF8 BOM if the response is parsed and returned as String, Closes android-async-http#659
1 parent b628f79 commit 447976b

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public abstract class AsyncHttpResponseHandler implements ResponseHandlerInterfa
9393
protected static final int BUFFER_SIZE = 4096;
9494

9595
public static final String DEFAULT_CHARSET = "UTF-8";
96+
public static final String UTF8_BOM = "\uFEFF";
9697
private String responseCharset = DEFAULT_CHARSET;
9798
private Handler handler;
9899
private boolean useSynchronousMode;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ protected Object parseResponse(byte[] responseBody) throws JSONException {
213213
String jsonString = getResponseString(responseBody, getCharset());
214214
if (jsonString != null) {
215215
jsonString = jsonString.trim();
216+
if (jsonString.startsWith(UTF8_BOM)) {
217+
jsonString = jsonString.substring(1);
218+
}
216219
if (jsonString.startsWith("{") || jsonString.startsWith("[")) {
217220
result = new JSONTokener(jsonString).nextValue();
218221
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ public void onFailure(int statusCode, Header[] headers, byte[] responseBytes, Th
114114
*/
115115
public static String getResponseString(byte[] stringBytes, String charset) {
116116
try {
117-
return stringBytes == null ? null : new String(stringBytes, charset);
117+
String toReturn = (stringBytes == null) ? null : new String(stringBytes, charset);
118+
if (toReturn != null && toReturn.startsWith(UTF8_BOM))
119+
return toReturn.substring(1);
120+
return toReturn;
118121
} catch (UnsupportedEncodingException e) {
119122
Log.e(LOG_TAG, "Encoding response into string failed", e);
120123
return null;

0 commit comments

Comments
 (0)