Java Android-在设备上显示UTF-8(俄语)
我正在我的android设备(Motorola Defy,2.1)上调试应用程序,该应用程序从网络上获取俄文HTML页面,但无法显示。它显示为。 HTML页面采用UTF-8格式(100%确定)。源代码:Java Android-在设备上显示UTF-8(俄语),java,android,unicode,utf-8,Java,Android,Unicode,Utf 8,我正在我的android设备(Motorola Defy,2.1)上调试应用程序,该应用程序从网络上获取俄文HTML页面,但无法显示。它显示为。 HTML页面采用UTF-8格式(100%确定)。源代码: HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet("http://m.rasp.yandex.ru/direction?direction=" + direction);
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);
HttpClient-HttpClient=newdefaulthttpclient();
HttpGet HttpGet=新的HttpGet(“http://m.rasp.yandex.ru/direction?direction=“+方向);
httpget.setHeader(“字符集”、“utf-8”);
ResponseHandler ResponseHandler=新BasicResponseHandler();
字符串html=httpclient.execute(httpget,responseHandler);
正常显示俄语文本需要什么?抱歉英语知识不好。我想您应该将
字符串html
从CP-1251或类似的smth解码为UTF-8(html.setHeader()
-可能会被忽略)
我的建议是复制返回的文本(比如从LogCat)并将其放入。因此,您将知道从Yandex返回的HTML的原始编码。httpget.setHeader(“charset”、“utf-8”)代码>没有任何意义
编码由BasicResponseHandler
确定。如果在内容类型
标题中未指定响应编码(如您的情况),基本应答处理程序
假定它是ISO-8859-1
,并且无法对其进行配置
因此,您需要实现自己的ResponseHandler
,它返回到另一种默认编码,如下所示:
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");
}
}
ResponseHandler ResponseHandler=新的ResponseHandler(){
公共字符串句柄响应(最终HttpResponse响应)
抛出HttpResponseException,IOException{
StatusLine StatusLine=response.getStatusLine();
if(statusLine.getStatusCode()>=300){
抛出新的HttpResponseException(statusLine.getStatusCode(),
statusLine.getReasonPhrase());
}
HttpEntity=response.getEntity();
返回实体==null?null:EntityUtils.toString(实体,“UTF-8”);
}
}
我研究过,操作系统试图在ISO8859-1中显示UTF-8。响应的内容在UTF-8中-它是100%,带有任何标题。