java查询天气报错
Exception in thread "Thread-1" com.alibaba.fastjson.JSONException: unterminated json string, pos 236011, line 1, column 236012
at homework4.MyWeather.getCityCode(MyWeather.java:49)
at homework4.MyWeather$1.backforresult(MyWeather.java:32)
at tool.NetData$1.run(NetData.java:26)
public class MyWeather {
Get gg=new Get();
private final static String KEY="1c017981fb804eae929c1a0613c948a6";
public MyWeather(){
}
public void show_weather() throws IOException {
while(true){
System.out.println("城市天气查询");
System.out.println("请输入城市名(中文):99——退出");
String city=gg.get();
if(city.equals("99")){
break;
}
city= URLEncoder.encode(city,"utf-8");
String uri="https://geoapi.qweather.com/v2/city/lookup?location="+city+"&key="+KEY;
NetData.getHttpResult(uri, new BackForResult() {
@Override
public void backforresult(String result) {
String citycode=getCityCode(result);
String uril="https://devapi.qweather.com/v7/"+"weather/now?location="+citycode+"&key="+KEY;
try {
NetData.getHttpResult(uril, new BackForResult() {
@Override
public void backforresult(String result) {
printWeather(result);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
private String getCityCode(String result){
JSONObject jsonObject= JSONObject.parseObject(result);
JSONArray locations= (JSONArray) jsonObject.get("location");
JSONObject location=locations.getJSONObject(0);
String citycode=location.getString("id");
return citycode;
}
private void printWeather(String result){
JSONObject json= JSONObject.parseObject(result);
JSONObject now=(JSONObject) json.get("now");
String temp=(String) now.get("temp");
System.out.println("当前温度:"+temp+"℃");
String text=now.getString("text");
System.out.println("当前天气:"+text);
}
}
49行代码,说明 text 字段,不是 String类型的,应该是个 JSONObject,你不能直接 getString吧