HttpEntity 响应中的字符呈现不正确
我正在做一个小应用程序,它接收一些带有 UTF-8 编码的 XML,浏览器中的相同 XML 可以正确呈现字符,而在 Android 中我有一些“垃圾”,例如。 WÅochy 代替 Włochy 或 Dwójka 代替 Dwójka。显然我做错了什么,有人可以帮我解决吗?
以下
String response = EntityUtils.toString( resEntity ).trim();
Log.i( CLASS_NAME, "RESPONSE=" + response );
//This returns response with incorrectly rendered characters eg. WÅochy instead of Włochy
是发送 POST 请求并接收响应的代码,它在 AsyncTask 中运行:
protected Document doInBackground(ServiceQuery... queries)
{
if ( m_sGateway == null ) return null;
Document result = null;
try
{
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost( m_sGateway );
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add( new BasicNameValuePair( "mdsXML", queries[0].getQueryString() ) );
UrlEncodedFormEntity ent = new UrlEncodedFormEntity( params, HTTP.UTF_8 );
post.setEntity( ent );
HttpResponse responsePOST = client.execute( post );
HttpEntity resEntity = responsePOST.getEntity();
if ( resEntity != null )
{
Log.i( CLASS_NAME, "contentLength:" + resEntity.getContentLength() );
Log.i( CLASS_NAME, "getContentEncoding():" + resEntity.getContentEncoding() );
Log.i( CLASS_NAME, "getContentEncoding().isChunked():" + resEntity.isChunked() );
Log.i( CLASS_NAME, "getContentEncoding().isRepeatable():" + resEntity.isRepeatable() );
String response = EntityUtils.toString( resEntity ).trim();
Log.i( CLASS_NAME, "RESPONSE=" + response );//This returns response with incorrectly rendered characters eg. WÅochy instead of Włochy
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = new ByteArrayInputStream( response.getBytes(HTTP.UTF_8) );
result = db.parse( is );
result.getDocumentElement().normalize();
}
}
catch (Exception e)
{
e.printStackTrace();
Log.e( CLASS_NAME, "doInBackground error." );
result = null;
}
return result;
}
I'm doing little app that receives some XML with UTF-8 encoding, the very same XML in browser renders characters correctly whereas in Android I've got some "Garbage" eg. WÅochy instead of Włochy or Dwójka instead of Dwójka. Apparently I'm doing something wrong, can anyone help me figuring it out?
Best regards
String response = EntityUtils.toString( resEntity ).trim();
Log.i( CLASS_NAME, "RESPONSE=" + response );
//This returns response with incorrectly rendered characters eg. WÅochy instead of Włochy
Following is the code that sends POST request and receives response, it is run in AsyncTask:
protected Document doInBackground(ServiceQuery... queries)
{
if ( m_sGateway == null ) return null;
Document result = null;
try
{
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost( m_sGateway );
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add( new BasicNameValuePair( "mdsXML", queries[0].getQueryString() ) );
UrlEncodedFormEntity ent = new UrlEncodedFormEntity( params, HTTP.UTF_8 );
post.setEntity( ent );
HttpResponse responsePOST = client.execute( post );
HttpEntity resEntity = responsePOST.getEntity();
if ( resEntity != null )
{
Log.i( CLASS_NAME, "contentLength:" + resEntity.getContentLength() );
Log.i( CLASS_NAME, "getContentEncoding():" + resEntity.getContentEncoding() );
Log.i( CLASS_NAME, "getContentEncoding().isChunked():" + resEntity.isChunked() );
Log.i( CLASS_NAME, "getContentEncoding().isRepeatable():" + resEntity.isRepeatable() );
String response = EntityUtils.toString( resEntity ).trim();
Log.i( CLASS_NAME, "RESPONSE=" + response );//This returns response with incorrectly rendered characters eg. WÅochy instead of Włochy
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = new ByteArrayInputStream( response.getBytes(HTTP.UTF_8) );
result = db.parse( is );
result.getDocumentElement().normalize();
}
}
catch (Exception e)
{
e.printStackTrace();
Log.e( CLASS_NAME, "doInBackground error." );
result = null;
}
return result;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Paul Grime 为我的问题提供了答案。
其中 resEntity 是 HttpEntity 实例。
credits for an answer to my problem goes to Paul Grime.
where resEntity is HttpEntity instance.