Question

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.

Was it helpful?

Solution

You may try using java.text.Normalizer or add <meta http-equiv="Content-Type" content="text/html charset=UTF-8" /> to your response.

Besides, I'd recommend you not to hardcode strings, but put them to resources and load them with Resources.getString(resId) instead - thus you would have potentially less problems with encodings.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top