开发者

Android - displaying UTF-8 (russian) on device

I'm debugging application on 开发者_运维知识库my android device (Motorola Defy, 2.1), the application gets Russian HTML page from network, but can't display it. It displays as link text. HTML page is in UTF-8 (100% sure). Source code:

HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://m.rasp.yandex.ru/direction?direction=" + direction);
        httpget.setHeader("charset", "utf-8");
        ResponseHandler<String> responseHandler = new BasicResponseHandler();

        String html = httpclient.execute(httpget, responseHandler);

What am I need for normal displaying of Russian text? Sorry for bad English knowledge.


httpget.setHeader("charset", "utf-8"); doesn't make any sense.

Encoding is determined by BasicResponseHandler. If response encoding is not specified in Content-Type header (as in your case), BasicResponseHandler assumes it to be ISO-8859-1, and it can't be configured.

So, you need to implement your own ResponseHandler that falls back to another default encoding, something like this:

ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
    public String handleResponse(final HttpResponse response)
        throws HttpResponseException, IOException {
        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() >= 300) {
            throw new HttpResponseException(statusLine.getStatusCode(),
                    statusLine.getReasonPhrase());
        }

        HttpEntity entity = response.getEntity();
        return entity == null ? null : EntityUtils.toString(entity, "UTF-8");
    }
} 


I suppose that you should decode String html from CP-1251 or smth like that to UTF-8 (html.setHeader() - migth be ignored).

My suggestion would be to copy returned text (let say from LogCat) and place it into ArtLebedev's decoder. So you will know original coding of HTML returned back from Yandex.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜