日语的UTF-8

时间:2014-06-06 10:50:35

标签: java utf

我在json响应中得到以下字符串

移动ピンポイントにお付き合いいただきましてありがとうございましたm(_ _)m来自P2235465 #colll_a http://t.co/LSxAxTGGd9

但是在utf-8转换后我得到了这个字符串

移A <•ハ”ンãƒã,¤ãƒ³ãƒã«ASA»~ãåã“A“AYAãã¾ã-ã| ã ,ã,ãããããããããã ((m m((((((((((m m m m m m m By By By By By By By By By m m m m(((((((((((((((((

我在java中为一些字符串进行转换我正在获得正确的输出。可能是UTF-16编码的情况,或者我做错了

以下是参考代码

String finalOutput = "";
 StringEntity inputRequest = new StringEntity(g.toJson(gt), "UTF-8");
        inputRequest.setContentType("application/json");
        postRequest.setEntity(inputRequest);
        HttpResponse response = httpClient.execute(postRequest);
        BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

        String outputContent;
        while ((outputContent = br.readLine()) != null) {
            if (finalOutput == null) {
                finalOutput = outputContent;
            } else {
                finalOutput += "\n" + outputContent;
            }
        }
            byte contentByte[];

    try {
        contentByte = finalOutput.getBytes("UTF-8");
        finalOutput = new String(contentByte,"UTF-8");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    return finalOutput;

1 个答案:

答案 0 :(得分:2)

有一个带有编码的InputStreamReader 的重载版本。只有在每个平台上都可以移植。

   BufferedReader br = new BufferedReader(
           new InputStreamReader((response.getEntity().getContent()),
               StandardCharsets.UTF_8)); // Or "UTF-8"

出于文体原因,最好使用StandardCharsets.UTF_8。

StringBuilder outputContent定义会更好;但正如其他人已经评论过的那样,字节和字符串之间的转换经常发生。也许考虑 ByteArrayOutputStream

    StringBuilder sb = new StringBuilder();
    String outputContent;
    while ((outputContent = br.readLine()) != null) {
        sb.append(outputContent).append('\n');
    }

    br.close();

    return= sb.toString(); // Final result, no more