Java获取城市当天气情况

package com.app.result.utils;


import com.alibaba.fastjson.JSON;
import com.app.admin.framework.statistics.bo.weather.bo.Daily;
import com.app.admin.framework.statistics.bo.weather.bo.Results;
import com.app.admin.framework.statistics.bo.weather.bo.WeatherBO;
import com.app.admin.framework.statistics.bo.weather.bo.WeatherDetail;
import com.app.util.GainUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

/**
 * @description:获取地区天气工具类
 */
@Slf4j
public class WeatherUtils {

    public static WeatherDetail getWeatherDetail() {
        WeatherBO weatherBO = JSON.parseObject(WeatherUtils.getWeatherByCityName("杭州"), WeatherBO.class);
        List<Results> results = weatherBO.getResults();
        if (CollectionUtils.isNotEmpty(results)){
            LocalDateTime localDateTime =LocalDateTime.now();
            WeatherDetail weatherDetail = new WeatherDetail();
            Results result = results.get(0);
            Daily daily = result.getDaily().get(0);
            weatherDetail.setCityName(result.getLocation().getName());
            weatherDetail.setDate(DateUtils.dateFormatMonth(daily.getDate()));

            if (localDateTime.getMinute()<10){
                String minutes="0"+localDateTime.getMinute();
                weatherDetail.setTimeStamp(localDateTime.getHour() + ":" + minutes + ":" + localDateTime.getSecond());
            }else{
                weatherDetail.setTimeStamp(localDateTime.getHour() + ":" + localDateTime.getMinute() + ":" + localDateTime.getSecond());
            }
            weatherDetail.setText_day(daily.getText_day());
            weatherDetail.setTemperature(daily.getLow()+"℃-"+daily.getHigh()+"℃");
            weatherDetail.setWeek(GainUtil.getWeekOfDate(DateUtils.dateFormatMonth(daily.getDate())));
            log.info("天气对象的信息是:{}",weatherDetail);
            return  weatherDetail;
        }
        return  null;
    }


    public static String getWeatherByCityName(String cityName){
        String weatherJSON = doGet("http://portalweather.comsys.net.cn/weather03/api/weatherService/getDailyWeather?cityName=" + cityName);
        return  weatherJSON;
    }

    public static String doGet(String url) {
        try {
            HttpClient client = new DefaultHttpClient();
            //发送get请求
            HttpGet request = new HttpGet(url);
            HttpResponse response = client.execute(request);
            /**请求发送成功,并得到响应**/
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                /**读取服务器返回过来的json字符串数据**/
                String strResult = EntityUtils.toString(response.getEntity());
                return strResult;
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}

也可以直接访问:http://portalweather.comsys.net.cn/weather03/api/weatherService/getDailyWeather?cityName=杭州

 附上接口响应:

// 20211202104929
// http://portalweather.comsys.net.cn/weather03/api/weatherService/getDailyWeather?cityName=%E6%9D%AD%E5%B7%9E

{
  "results": [
    {
      "location": {
        "id": "WTMKQ069CCJ7",
        "name": "杭州",
        "country": "CN",
        "path": "杭州,杭州,浙江,中国",
        "timezone": "Asia/Shanghai",
        "timezone_offset": "+08:00"
      },
      "daily": [
        {
          "date": "2021-12-02",
          "text_day": "晴",
          "code_day": "0",
          "text_night": "晴",
          "code_night": "1",
          "high": "12",
          "low": "1",
          "rainfall": "0.00",
          "precip": "0.00",
          "wind_direction": "西南",
          "wind_direction_degree": "225",
          "wind_speed": "23.4",
          "wind_scale": "4",
          "humidity": "64"
        },
        {
          "date": "2021-12-03",
          "text_day": "晴",
          "code_day": "0",
          "text_night": "晴",
          "code_night": "1",
          "high": "15",
          "low": "3",
          "rainfall": "0.00",
          "precip": "0.00",
          "wind_direction": "西北",
          "wind_direction_degree": "315",
          "wind_speed": "23.4",
          "wind_scale": "4",
          "humidity": "65"
        },
        {
          "date": "2021-12-04",
          "text_day": "晴",
          "code_day": "0",
          "text_night": "多云",
          "code_night": "4",
          "high": "14",
          "low": "3",
          "rainfall": "0.00",
          "precip": "0.00",
          "wind_direction": "无持续风向",
          "wind_direction_degree": "",
          "wind_speed": "3.0",
          "wind_scale": "1",
          "humidity": "69"
        }
      ],
      "last_update": "2021-12-02T08:00:00+08:00"
    }
  ]
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java小白笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值