Luk*_*ela 2 android httpresponse utf-8 http-post character-encoding
我正在做的小应用程序接收一些带有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
Run Code Online (Sandbox Code Playgroud)
以下是发送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;
}
Run Code Online (Sandbox Code Playgroud)
Luk*_*ela 11
对我的问题的答案的信用去Paul Grime.
import org.apache.http.util.EntityUtils;
String response = EntityUtils.toString( resEntity, HTTP.UTF_8 );
Run Code Online (Sandbox Code Playgroud)
其中resEntity是HttpEntity实例.
| 归档时间: |
|
| 查看次数: |
7499 次 |
| 最近记录: |