通过各个平台提供的API接口,获取最近七天的天气预报。
经过测试,基于每天免费调用次数、是否需要注册,选择腾讯的API接口,目前不需要注册,不限制调用次数。
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
public class WeatherUtil {
/**
* @param province **省
* @param city **市
* @return 基于自身业务,组装的数据
*/
public static ArrayList<String> getWeekWeather(String province, String city) {
ArrayList<String> weatherList = new ArrayList<>();
try {
String path = "https://wis.qq.com/weather/common?source=pc&weather_type=observe|forecast_24h|air&province=" + province + "&city=" + city;
URL url = new URL(path);
URLConnection connectionData = url.openConnection();
connectionData.setConnectTimeout(1000);
BufferedReader br = new BufferedReader(new InputStreamReader(connectionData.getInputStream(), "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null)
sb.append(line);
String datas = sb.toString();
System.out.println(datas);
JSONObject weekWeatherJSONObject = JSON.parseObject(datas).getJSONObject("data").getJSONObject("forecast_24h");
for (int i = 0; i < weekWeatherJSONObject.size(); i++) {
JSONObject jsonObject = weekWeatherJSONObject.getJSONObject(i+"");
String day_weather = jsonObject.getString("day_weather");
String day_weather_short = jsonObject.getString("day_weather_short");
String day_wind_direction = jsonObject.getString("day_wind_direction");
String day_wind_power = jsonObject.getString("day_wind_power");
String max_degree = jsonObject.getString("max_degree");
String min_degree = jsonObject.getString("min_degree");
String night_weather = jsonObject.getString("night_weather");
String night_weather_short = jsonObject.getString("night_weather_short");
String night_wind_direction = jsonObject.getString("night_wind_direction");
String night_wind_power = jsonObject.getString("night_wind_power");
String time = jsonObject.getString("time");
String result = "日期:" + time + ", 最高气温:" + max_degree + ", 最低气温" + min_degree + ", 白天天气:" + day_weather + ", 白天风向:" + day_wind_direction + ", 白天风力:" + day_wind_power + ", 夜间天气:" + night_weather + ", 夜间风向:" + night_wind_direction + ", 夜间风力:" + night_wind_power;
weatherList.add(result);
}
} catch (Exception e) {
e.printStackTrace();
}
return weatherList;
}
/**
* @param province **省
* @param city **市
* @param county **区
* @return 基于自身业务,组装的数据
*/
public static ArrayList<String> getWeekWeather(String province, String city, String county) {
ArrayList<String> weatherList = new ArrayList<>();
try {
String path = "https://wis.qq.com/weather/common?source=pc&weather_type=observe|forecast_24h|air&province=" + province + "&city=" + city + "&county=" + county;
URL url = new URL(path);
URLConnection connectionData = url.openConnection();
connectionData.setConnectTimeout(1000);
BufferedReader br = new BufferedReader(new InputStreamReader(connectionData.getInputStream(), "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null)
sb.append(line);
String datas = sb.toString();
System.out.println(datas);
JSONObject weekWeatherJSONObject = JSON.parseObject(datas).getJSONObject("data").getJSONObject("forecast_24h");
for (int i = 0; i < weekWeatherJSONObject.size(); i++) {
JSONObject jsonObject = weekWeatherJSONObject.getJSONObject(i+"");
String day_weather = jsonObject.getString("day_weather");
String day_weather_short = jsonObject.getString("day_weather_short");
String day_wind_direction = jsonObject.getString("day_wind_direction");
String day_wind_power = jsonObject.getString("day_wind_power");
String max_degree = jsonObject.getString("max_degree");
String min_degree = jsonObject.getString("min_degree");
String night_weather = jsonObject.getString("night_weather");
String night_weather_short = jsonObject.getString("night_weather_short");
String night_wind_direction = jsonObject.getString("night_wind_direction");
String night_wind_power = jsonObject.getString("night_wind_power");
String time = jsonObject.getString("time");
String result = "日期:" + time + ", 最高气温:" + max_degree + ", 最低气温" + min_degree + ", 白天天气:" + day_weather + ", 白天风向:" + day_wind_direction + ", 白天风力:" + day_wind_power + ", 夜间天气:" + night_weather + ", 夜间风向:" + night_wind_direction + ", 夜间风力:" + night_wind_power;
weatherList.add(result);
}
} catch (Exception e) {
e.printStackTrace();
}
return weatherList;
}
public static void main(String[] args) throws IOException {
String province = "湖南省";
String city = "长沙市";
String county = "岳麓区";
ArrayList<String> weekWeather = getWeekWeather(province, city, county);
for (int i = 0; i < weekWeather.size(); i++) {
System.out.println(weekWeather.get(i));
}
}
}