首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android Java UTF-8 HttpClient问题

Android Java UTF-8 HttpClient问题
EN

Stack Overflow用户
提问于 2010-12-19 05:50:49
回答 4查看 31.3K关注 0票数 16

我有一个从网页抓取的JSON数组的奇怪的字符编码问题。服务器将发回此标头:

Content-Type text/javascript;charset=UTF-8

此外,我还可以在Firefox或任何浏览器中查看JSON输出,并且Unicode字符显示正确。响应有时会包含来自另一种语言的带有重音符号等的单词。然而,当我在Java中把它拉下并放到一个字符串中时,我得到了那些奇怪的问号。下面是我的代码:

代码语言:javascript
复制
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "utf-8");
params.setBooleanParameter("http.protocol.expect-continue", false);

HttpClient httpclient = new DefaultHttpClient(params);

HttpGet httpget = new HttpGet("http://www.example.com/json_array.php");
HttpResponse response;
    try {
        response = httpclient.execute(httpget);

        if(response.getStatusLine().getStatusCode() == 200){
            // Connection was established. Get the content. 

            HttpEntity entity = response.getEntity();
            // If the response does not enclose an entity, there is no need
            // to worry about connection release

            if (entity != null) {
                // A Simple JSON Response Read
                InputStream instream = entity.getContent();
                String jsonText = convertStreamToString(instream);

                Toast.makeText(getApplicationContext(), "Response: "+jsonText, Toast.LENGTH_LONG).show();

            }

        }


    } catch (MalformedURLException e) {
        Toast.makeText(getApplicationContext(), "ERROR: Malformed URL - "+e.getMessage(), Toast.LENGTH_LONG).show();
        e.printStackTrace();
    } catch (IOException e) {
        Toast.makeText(getApplicationContext(), "ERROR: IO Exception - "+e.getMessage(), Toast.LENGTH_LONG).show();
        e.printStackTrace();
    } catch (JSONException e) {
        Toast.makeText(getApplicationContext(), "ERROR: JSON - "+e.getMessage(), Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }

private static String convertStreamToString(InputStream is) {
    /*
     * To convert the InputStream to String we use the BufferedReader.readLine()
     * method. We iterate until the BufferedReader return null which means
     * there's no more data to read. Each line will appended to a StringBuilder
     * and returned as String.
     */
    BufferedReader reader;
    try {
        reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    StringBuilder sb = new StringBuilder();

    String line;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

正如您所看到的,我在InputStreamReader上指定了UTF-8,但是每次我通过Toast查看返回的JSON文本时,它都有奇怪的问号。我在想,我需要将InputStream发送到byte[]吗?

提前感谢您的帮助。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-12-19 06:32:28

试试这个:

代码语言:javascript
复制
if (entity != null) {
    // A Simple JSON Response Read
    // InputStream instream = entity.getContent();
    // String jsonText = convertStreamToString(instream);

    String jsonText = EntityUtils.toString(entity, HTTP.UTF_8);

    // ... toast code here
}
票数 40
EN

Stack Overflow用户

发布于 2014-07-29 01:11:38

这只是因为您的convertStreamToString没有遵守HttpRespnose中设置的编码。如果查看EntityUtils.toString(entity, HTTP.UTF_8)内部,您将看到EntityUtils首先确定HttpResponse中是否设置了编码,如果有,则EntityUtils使用该编码。只有在实体中没有设置编码时,它才会回退到参数中传递的编码(在本例中是HTTP.UTF_8)。

因此,您可以说您的HTTP.UTF_8是在参数中传递的,但它永远不会被使用,因为它是错误的编码。下面是使用EntityUtils中的帮助器方法更新您的代码。

代码语言:javascript
复制
           HttpEntity entity = response.getEntity();
           String charset = getContentCharSet(entity);
           InputStream instream = entity.getContent();
           String jsonText = convertStreamToString(instream,charset);

    private static String getContentCharSet(final HttpEntity entity) throws ParseException {
    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity may not be null");
    }
    String charset = null;
    if (entity.getContentType() != null) {
        HeaderElement values[] = entity.getContentType().getElements();
        if (values.length > 0) {
            NameValuePair param = values[0].getParameterByName("charset");
            if (param != null) {
                charset = param.getValue();
            }
        }
    }
    return TextUtils.isEmpty(charset) ? HTTP.UTF_8 : charset;
}



private static String convertStreamToString(InputStream is, String encoding) {
    /*
     * To convert the InputStream to String we use the
     * BufferedReader.readLine() method. We iterate until the BufferedReader
     * return null which means there's no more data to read. Each line will
     * appended to a StringBuilder and returned as String.
     */
    BufferedReader reader;
    try {
        reader = new BufferedReader(new InputStreamReader(is, encoding));
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    StringBuilder sb = new StringBuilder();

    String line;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}
票数 1
EN

Stack Overflow用户

发布于 2015-12-02 01:53:14

阿基米德的答案是正确的。但是,只需在HTTP请求中提供额外的标头即可:

代码语言:javascript
复制
Accept-charset: utf-8

不需要删除任何东西或使用任何其他库。

例如,

代码语言:javascript
复制
GET / HTTP/1.1
Host: www.website.com
Connection: close
Accept: text/html
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.10 Safari/537.36
DNT: 1
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: utf-8

您的请求很可能没有任何Accept-Charset头。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4480363

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档