Skip to content

Response string charset option #286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/com/loopj/android/http/AsyncHttpResponseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

/**
* Used to intercept and handle the responses from requests made using
* {@link AsyncHttpClient}. The {@link #onSuccess(String)} method is
* {@link AsyncHttpClient}. The {@link #onSuccess(String)} method is
* designed to be anonymously overridden with your own response handling code.
* <p>
* Additionally, you can override the {@link #onFailure(Throwable, String)},
Expand Down Expand Up @@ -76,6 +76,7 @@ public class AsyncHttpResponseHandler {
protected static final int FINISH_MESSAGE = 3;

private Handler handler;
private String charset = "UTF-8";

/**
* Creates a new AsyncHttpResponseHandler
Expand All @@ -92,7 +93,6 @@ public void handleMessage(Message msg){
}
}


//
// Callbacks to be overridden, typically anonymously
//
Expand Down Expand Up @@ -213,6 +213,16 @@ protected void handleMessage(Message msg) {
}
}

/**
* Sets the charset for the response string. If not set, the default is UTF-8.
* @see <a href="http://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html">Charset</a>
*
* @param charset to be used for the response string.
*/
public void setCharset(final String charset) {
this.charset = charset;
}

protected void sendMessage(Message msg) {
if(handler != null){
handler.sendMessage(msg);
Expand Down Expand Up @@ -242,7 +252,7 @@ void sendResponseMessage(HttpResponse response) {
HttpEntity temp = response.getEntity();
if(temp != null) {
entity = new BufferedHttpEntity(temp);
responseBody = EntityUtils.toString(entity, "UTF-8");
responseBody = EntityUtils.toString(entity, charset);
}
} catch(IOException e) {
sendFailureMessage(e, (String) null);
Expand Down