最近在学习Java的过程中,写了一个查询天气的小程序,发现输出为乱码,尝试了几种方法,发现一个奇怪又简单的方法解决了这个问题,只需要编码一个字段即可,其它字段自动重新编码,理论上讲Windows使用GBK编码方式,但是我尝试GBK编码却失败了,Idea默认为UTF-8,再次尝试使用Standard UTF-8,居然成功了,奇葩就在这里,一时无法理解,分享给大家。
修改编码方式后:
public static void query(String id) throws Exception {
try {
URL url = new URL("https://restapi.amap.com/v3/weather/weatherInfo?city=" + id + "&key=23879d193adefa8671ee71ea8124915c");
System.out.println("Author: 张莽 (Zhang Mang) @10.10.2022, Homepage: www.pavogroup.top");
System.out.println("I will get data from: " + url);
InputStream is = url.openConnection().getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
String line = br.readLine();
// System.out.println("Return Data: " + line);
JSONObject object = JSONObject.fromObject(line);
JSONArray array = object.getJSONArray("lives");
object = array.getJSONObject(0);
String city = object.getString("city");
String province = object.getString("province");
String weather = object.getString("weather");
String windpower = object.getString("windpower");
String humidity = object.getString("humidity");
String reporttime = object.getString("reporttime");
String winddirection = object.getString("winddirection");
String temperature = object.getString("temperature");
System.out.println("(今天的天气:)Today Weather: ==================================================== ");
byte[] b = province.getBytes(StandardCharsets.UTF_8);// 编码
String provinceG = new String(b, StandardCharsets.UTF_8);
System.out.println("Province: " + provinceG);
System.out.println("City: " + city);
System.out.println("Weather: " + weather);
System.out.println("WindPower: " + windpower);
System.out.println("Humidity: " + humidity);
System.out.println("WindDirection: " + winddirection);
System.out.println("Temperature: " + temperature);
System.out.println("ReportTime: " + reporttime);
System.out.println("==================================================== ");
} catch (Exception e) {
System.out.println("JSONException Error: " + e.getMessage());
// System.exit(1);
}
}
public static void queryCasts(String id) throws Exception {
for (int i = 0; i < 4; i++) {
try {
URL url = new URL("https://restapi.amap.com/v3/weather/weatherInfo?city=" + id + "&key=23879d193adefa8671ee71ea8124915c&extensions=all");
InputStream is = url.openStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
String line = br.readLine();
// System.out.println("Return Data: " + line);
JSONObject object = JSONObject.fromObject(line);
JSONArray array = object.getJSONArray("forecasts");
object = array.getJSONObject(0);
JSONArray array1 = object.getJSONArray("casts");
object = array1.getJSONObject(i);
String date = object.getString("date");
String week = object.getString("week");
String dayweather = object.getString("dayweather");
String nightweather = object.getString("nightweather");
String daytemp = object.getString("daytemp");
String nighttemp = object.getString("nighttemp");
String daywind = object.getString("daywind");
String nightwind = object.getString("nightwind");
String daypower = object.getString("daypower");
String nightpower = object.getString("nightpower");
System.out.println("Forcast weather: *********************************************** ");
System.out.println("date: " + date);
System.out.println("week: " + week);
byte[] b = dayweather.getBytes(StandardCharsets.UTF_8);// 编码
String dayweatherG = new String(b, StandardCharsets.UTF_8); //解码
System.out.println("(天气:)daywather: " + dayweatherG);
System.out.println("nightweather: " + nightweather);
System.out.println("daytemp: " + daytemp);
System.out.println("nighttemp: " + nighttemp);
System.out.println("daywind: " + daywind);
System.out.println("nightwind: " + nightwind);
System.out.println("daypower: " + daypower);
System.out.println("nightpower: " + nightpower);
System.out.println(" ***************************************************************");
} catch (Exception e) {
System.out.println("JSONException Error: " + e.getMessage());
// System.exit(2);
}
}
}
标签:Java,String,getString,解决方案,object,System,乱码,println,out From: https://www.cnblogs.com/managechina/p/16759037.html