我正在做的小应用程序接收一些带有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;
}
答案 0 :(得分:11)
我的问题的答案可以归功于Paul Grime。
import org.apache.http.util.EntityUtils;
String response = EntityUtils.toString( resEntity, HTTP.UTF_8 );
其中resEntity是HttpEntity实例。