HttpEntity 响应中的字符呈现不正确

发布于 2024-12-05 05:54:53 字数 2235 浏览 2 评论 0原文

我正在做一个小应用程序,它接收一些带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海夜未眠 2024-12-12 05:54:53

Paul Grime 为我的问题提供了答案。

import org.apache.http.util.EntityUtils;

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

其中 resEntity 是 HttpEntity 实例。

credits for an answer to my problem goes to Paul Grime.

import org.apache.http.util.EntityUtils;

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

where resEntity is HttpEntity instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文