UTF-8字符(è;)中的java转换问题
嗨,我对java有问题。。。json以字符串数组的形式正确到达,但字符(is)是。。。有人能帮我转换成UTF-8和字符串吗 谢谢UTF-8字符(è;)中的java转换问题,java,json,Java,Json,嗨,我对java有问题。。。json以字符串数组的形式正确到达,但字符(is)是。。。有人能帮我转换成UTF-8和字符串吗 谢谢 private static void loadProperties() throws Exception { String properties = null; HttpsURLConnection con = (HttpsURLConnection) new URL(getEndpoint()).openConnection();
private static void loadProperties() throws Exception {
String properties = null;
HttpsURLConnection con = (HttpsURLConnection) new URL(getEndpoint()).openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer res = new StringBuffer();
while((inputLine = in.readLine()) != null) {
res.append(inputLine);
}
in.close();
JsonObject jsonObject = new JsonParser().parse(res.toString()).getAsJsonObject();
if(jsonObject.has("error")) {
JsonObject error = jsonObject.get("error").getAsJsonObject();
if(error.has("errorCode") && "1000".equals(error.get("errorCode").getAsString()) && jsonObject.has("data")) {
properties = jsonObject.get("data").getAsJsonArray().toString();
log("PropertiesUtils.loadProperties: Properties = %s", properties);
setProperties(properties);
} else {
throw new Exception("PropertiesUtils.loadProperties: Error while calling properties endpoint");
}
} else {
throw new Exception("PropertiesUtils.loadProperties: Error while calling properties endpoint");
}
}
您的错误如下:
new InputStreamReader(con.getInputStream())
这将采用InputStream
流传输HTTP响应(即纯字节流),并使用默认编码将其重新解释为文本(因为您没有指定编码)
默认编码完全取决于您的计算机、操作系统和设置,与远程服务器可能向您发送数据的编码完全无关
您需要使用以获取服务器通知您的编码:
new InputStreamReader(con.getInputStream(), con.getContentEncoding());
请注意,您不需要执行所构建的整个读入字符串循环:您可以直接使用中的
BufferedReader作为参数调用。con.getContentEncoding()我尝试使用此函数……但是ide给了我一个error@emaAme:您是如何尝试的,它会给您带来什么错误?不要仅仅将错误消息视为危险信号。读它们。