Wednesday, April 18, 2012

Android doesn't have appropriate encoding

I have httpservice on android device. It forms response as html page to some browser. So it works fine but if I use other languages (for example Russian), server returns unreadable text instead of Russian symbols. I know that default encoding on Android is UTF-8. I've tried every encoding available on the Android (For example windows-1251, Big5(Chinese), UTF-16 and so on) but it returned wrong result at all. Here is some code which show you what I've tried to do:



@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext httpContext) throws HttpException, IOException {

HttpEntity entity = new EntityTemplate(new ContentProducer() {
public void writeTo(final OutputStream outstream) throws IOException {

OutputStreamWriter writer = new OutputStreamWriter(outstream, "windows-1251");
String resp = "<html><body>Hello ??????</body></html>";
StringEntity se = new StringEntity(resp, "windows-1251");
se.writeTo(outstream);
//writer.write(resp);
//writer.flush();
}
});
response.setHeader("Context-Type", "text/html");
response.setEntity(entity);
}


So in browser I see next:



Hello Привет


What do I wrong? Please, answer me.
I'll appreciate for any suggestions. Thanks.





No comments:

Post a Comment