字符HttpEntity响应不正确地呈现 [英] Characters rendered incorrectly in HttpEntity response

查看:281
本文介绍了字符HttpEntity响应不正确地呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一些接收XML使用UTF-8编码的小应用程序,浏览器非常相同的XML正确呈现的字符,而在Android的我有一些垃圾,例如。 WÅochy代替Włochy或Dwójka而不是Dwójka。显然,我做错了什么,谁能帮我想出来的?

最好的问候

 字符串响应= EntityUtils.toString(resEntity).trim();
Log.i(CLASS_NAME,响应=+响应);
//这将返回响应与呈现不正确的字符如: WÅochy代替Włochy

以下是code发送POST请求并接收响应,它的AsyncTask运行:

 保护文档doInBackground(ServiceQuery ...查询)
{
    如果(m_sGateway == NULL)返回NULL;    文档结果= NULL;    尝试
    {
        HttpClient的客户端=新DefaultHttpClient();        HttpPost后=新HttpPost(m_sGateway);        清单<&的NameValuePair GT; PARAMS =新的ArrayList<&的NameValuePair GT;();
        params.add(新BasicNameValuePair(mdsXML,查询[0] .getQueryString()));        UrlEn codedFormEntity ENT =新UrlEn codedFormEntity(参数,可以HTTP.UTF_8);
        post.setEntity(ENT);
        HTT presponse responsePOST = client.execute(岗位);        HttpEntity resEntity = responsePOST.getEntity();        如果(resEntity!= NULL)
        {
            Log.i(CLASS_NAMECONTENTLENGTH:+ resEntity.getContentLength());
            Log.i(CLASS_NAMEgetContentEncoding():+ resEntity.getContentEncoding());
            Log.i(CLASS_NAMEgetContentEncoding()isChunked():+ resEntity.isChunked());
            Log.i(CLASS_NAMEgetContentEncoding()isRepeatable():+ resEntity.isRepeatable());            串响应= EntityUtils.toString(resEntity).trim();            Log.i(CLASS_NAME,响应=+响应); //这将返回响应与呈现不正确的字符如: WÅochy代替Włochy
            DBF的DocumentBuilderFactory = DocumentBuilderFactory.newInstance();
            的DocumentBuilder分贝= dbf.newDocumentBuilder();            InputStream为=新ByteArrayInputStream进行(response.getBytes(HTTP.UTF_8));
            结果= db.parse(是);
            result.getDocumentElement()正常化()。
        }    }
    赶上(例外五)
    {
        e.printStackTrace();
        Log.e(CLASS_NAME,doInBackground错误。);
        结果= NULL;
    }    返回结果;
}


解决方案

的答案,我的问题学分去污秽物保

 进口org.apache.http.util.EntityUtils;字符串的响应= EntityUtils.toString(resEntity,HTTP.UTF_8);

其中resEntity是HttpEntity实例。

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;
}

解决方案

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.

这篇关于字符HttpEntity响应不正确地呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆