URLConnection字符编码

时间:2012-03-03 19:43:23

标签: java encoding httpurlconnection

我正在尝试读取JSON字符串:

{
  "also_known_as": [
    "Сильвестр Сталлоне"
  ],

  "birthday": "1946-07-06",
  "deathday": "",
}

通过HTTP。

我有以下代码:

URL url = new URL("url");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Accept-Charset", "UTF-8");//connection.setRequestProperty("Accept-Charset", "ISO-8859-1");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = "";
StringWriter writer = new StringWriter();
while((line=reader.readLine())!=null){
    writer.write(line);
}
reader.close();
writer.close();     
connection.disconnect();
System.out.println(writer.toString());

但是它在控制台中打印字符串:

{
  "also_known_as": [
    "СильвеÑ?Ñ‚Ñ€ Сталлоне"
  ],

  "birthday": "1946-07-06",
  "deathday": "",
}

我也尝试过:

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));//BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "ISO-8859-1"));

但没有运气。

我的问题是如何设置URLConnection的字符编码?

任何信息都对我很有帮助。

的问候。


使用Apache IOUtils我试过这个:

StringWriter writer = new StringWriter();
IOUtils.copy(connection.getInputStream(), writer, "UTF-8");

但它在eclipse的控制台中打印出相同的结果。


使用Apache HttpClient:

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet getRequest = new HttpGet("http://api.themoviedb.org/3/person/16483?api_key=23e89da030a0ee8b25aaed20950a0c25");
getRequest.addHeader("accept", "application/json");
HttpResponse response = httpClient.execute(getRequest);
StringWriter writer = new StringWriter();
IOUtils.copy(response.getEntity().getContent(), writer, "UTF-8");
System.out.println(writer.toString());

同样的结果。

2 个答案:

答案 0 :(得分:5)

只是将我的评论弹出一个答案,结果证明是原因:控制台的字符集是Cp1252,所以输出正确但显示不正确。

答案 1 :(得分:1)

制作:

new InputStreamReader(connection.getInputStream(), new Charset("UTF-8"))

即。指定那个charset。