Java 通过HttpURLConnection获取URL接口的返回数据中含有少数民族语言变成了乱码 5C
各位大神


private static String getJson(String url){

// String url = "http://data1.cnr.cn/minzu_hanyuzhuan/phpencode.php?word="+para;
URL oldUrl;
StringBuffer buffer = null;
try {
oldUrl = new URL(url);
HttpURLConnection con = (HttpURLConnection) oldUrl.openConnection();
con.setRequestMethod("GET");
con.addRequestProperty("Content-Type",
"application/json;charset=utf-8");
con.connect();
InputStream in = con.getInputStream();
BufferedReader bf=new BufferedReader(new InputStreamReader(in,"UTF-8"));

buffer=new StringBuffer();

String line="";

while((line=bf.readLine())!=null){

buffer.append(line);

}

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

    String json = buffer.toString();
    return json;
}


该方法得到的json字符串中的少数民族语言都是乱码,该怎么解决这个问题?

4个回答

对字符串encode、decode编码解码

UTF-8对于中文支持没有GB好,但是utf-8更常用。

http://blog.csdn.net/luckily01/article/details/7723025

 httpurlconnection.setRequestProperty("Accept-Charset", "utf-8");
   httpurlconnection.setRequestProperty("contentType", "utf-8");

请求的字符集是否是网页使用的字符集?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!