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"
}
]
}