【问题标题】:Characters rendered incorrectly in HttpEntity responseHttpEntity 响应中的字符呈现不正确
【发布时间】:2011-11-19 20:16:01
【问题描述】:

我正在做一个小应用程序,它接收一些带有 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;
}

【问题讨论】:

  • 检查返回的响应是否包含字符编码,并且该响应实际上是使用该编码编码的。如果这看起来不错,那么尝试强制 EntityUtils 使用 UTF-8 - developer.android.com/reference/org/apache/http/util/…
  • 我会检查它,响应 XML 在处理指令 中具有 UTF-8 编码,从浏览器运行的相同服务也可以正确显示字符。
  • 嗨,保罗,它有帮助,当我这样做时:Log.i(CLASS_NAME, "getContentCharSet="+EntityUtils.getContentCharSet( resEntity )); 它返回 null,然后我使用:String response = EntityUtils.toString( resEntity, HTTP.UTF_8 ).trim(); 从实体中检索了字符串,并且我的口音开始正确显示:) 修剪是只是因为我在 PI 之前有一些白色字符。
  • 我的荣幸 - 很高兴它正在工作!

标签: android utf-8 character-encoding http-post httpresponse


【解决方案1】:

我的问题的答案归功于 Paul Grime。

import org.apache.http.util.EntityUtils;

String response = EntityUtils.toString( resEntity, HTTP.UTF_8 );

其中 resEntity 是 HttpEntity 实例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-30
    • 2014-04-29
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多