近7天天气预报---Java实现

通过各个平台提供的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));
        }
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值