java - Android Java UTF-8 HttpClient 问题

标签 java android httpclient

我在使用从网页抓取的 JSON 数组时遇到了奇怪的字符编码问题。服务器正在发回此 header :

内容类型文本/javascript; charset=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 发送到 byte[] 吗?

提前感谢您的帮助。

最佳答案

试试这个:

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
}

关于java - Android Java UTF-8 HttpClient 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4480363/

相关文章:

java - 如何在 IntelliJ 中的有效位置根项目?

android - GridView : How to fill entire space with text view

android - SeekBar 和 EditText 相互更改导致的 Stackoverflow 错误

Android 服务 onStartCommand 从未调用过

java - 使用 Apache HttpClient 将原始字节添加到发布参数

java - 棘手 Action 的抽象实现,例如,呃,行走

java - Java 指纹识别器

PHP GuzzleHttp。如何使用参数发出 post 请求?

java - 为什么我收到 HTTP 400 错误请求

java - 从外部Java屏幕资源获取 "image"