public static String getWeather(String adcode,String key){
String url = "https://restapi.amap.com/v3/weather/weatherInfo?city="+adcode+"&key="+key;
String s = HttpsUtil.get(url);
JSONObject object = JSONObject.parseObject(s);
WeatherModel jb = (WeatherModel)JSONObject.toJavaObject(object, WeatherModel.class);
LivesModel livesModel = jb.getLives().get(0);
int c =Integer.valueOf(livesModel.getTemperature());
SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
String currSun = dateFm.format(new Date());
String r = null;
if (c<=26 && c>=18){
r="今天温度适宜";
}else if (c>26){
r="今天温度较高,注意防晒";
}else if(c<18){
r="今天温度较低,注意保暖";
}
String zong = "日期: "+livesModel.getReporttime()+" "+currSun+"\n"+
"所在城市: "+livesModel.getCity()+"\n"+
"天气: " + livesModel.getWeather()+"\n"+
"温度: "+ livesModel.getTemperature()+" "+r+"\n"+
"风向: "+ livesModel.getWinddirection()+"风"+"\n"+
"风级: "+livesModel.getWindpower();
return zong;
}
private static String getCityCode(String adress,String key){
String url = "https://restapi.amap.com/v3/geocode/geo?address="+adress+"&key="+key;
String s = HttpsUtil.get(url);
JSONObject object = JSONObject.parseObject(s);
System.out.println(object);
JSONArray array = (JSONArray) object.get("geocodes");
JSONObject arrayObject = (JSONObject) array.get(0);
String adcode = arrayObject.get("adcode").toString();
return adcode;
}
public static void main(String[] args) {
System.out.println(getCityCode("河南省郑州市","b2f301c36******77d21"));
System.out.println(getWeather(getCityCode("河南省郑州市","b2f3*****7d21"),"b2f3*******7d21"));
}
|