package com.securityControl.task.util; import com.securityControl.task.domain.vo.WeatherVo; //import org.json.JSONArray; //import org.json.JSONObject; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * @Auther: ccw * @Date: 2022/07/08/13:54 * @description: 天气api接口工具类 */ public class WeatherUtil { public static List getTodayWeather(String cityId) throws IOException, NullPointerException { // wthrcdn.etouch.cn/weather_mini?city=北京 // api.yytianqi.com/observe?city=CH220101&key=qm7e03wbb42du4vn // 连接中央气象台的API // https://www.tianqiapi.com/api/?version=v1&cityid="+cityId+"&appid=62429591&appsecret=XgA9EDAS String url = "https://www.tianqiapi.com/api/?version=v1&cityid=" + cityId + "&appid=62429591&appsecret=XgA9EDAS "; String str; List weatherList = new ArrayList<>(); try { // str = HttpsUntil.httpsRequest(url, "GET", null); // System.err.println(str); // JSONObject jsonData = new JSONObject(str); // JSONArray arr = jsonData.getJSONArray("data"); // if (arr != null && arr.length() > 0) { // JSONObject obj = arr.getJSONObject(0); // JSONArray hoursList = obj.getJSONArray("hours"); // if (hoursList != null && hoursList.length() > 0) { // for (int i = 0; i < hoursList.length(); i++) { // JSONObject jsonObject = hoursList.getJSONObject(i); // WeatherVo weather = new WeatherVo(); // weather.setName(jsonData.getString("city")); // weather.setWeather(obj.getString("wea")); // weather.setTempMax(obj.getString("tem1")); // weather.setTempMin(obj.getString("tem2")); // weather.setHours(jsonObject.getString("hours")); // weather.setRealTimeWeather(jsonObject.getString("wea")); // weather.setRealTimeTemp(jsonObject.getString("tem") + "℃"); // weather.setCreateTime(obj.getString("date")); // weatherList.add(weather); // } // } // } } catch (Exception e) { System.err.println("获取天气接口数据失败"); e.printStackTrace(); } return weatherList; } }