Android Java UTF-8 HttpClient问题

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (82)

我从网页抓取的JSON数组中遇到了奇怪的字符编码问题。服务器正在发回这个标题:

Content-Type text / javascript; 字符集= UTF-8

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

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到一个字节[],而不是?

提问于
用户回答回答于

尝试这个:

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
}
用户回答回答于

我的猜测是:

  1. 服务器在流的开始处放置一个UTF字节顺序标记(BOM)。标准的Java UTF-8字符解码器不会删除物料清单,因此它有可能会在结果字符串中结束。(然而,EntityUtils的代码似乎也没有对BOM做任何事情。)
  2. convertStreamToString正在一次读取字符流中的一行,并使用硬连线'\n'作为行尾标记重新组装它。如果你要将其写入外部文件或应用程序,则应该使用平台特定的行尾标记。

扫码关注云+社区