免费天气API接口
package com.panxg.api.weather.one; import java.util.HashMap; / * @author panxg * @date 2021年06月21日 10:43 */ public class WeatherConfig {
public static final String URL = "https://tianqiapi.com/api"; public static final String APPID = ""; public static final String APPSECRET = "giWz4pEF"; /固定值: v6 每个接口的version值都不一样*/ public static final String VERSION = "v6"; public static HashMap<String, Object> getConfigMap(){
HashMap<String, Object> headers = new HashMap<>(7); headers.put("version", WeatherConfig.VERSION); headers.put("appid", WeatherConfig.APPID); headers.put("appsecret", WeatherConfig.APPSECRET); return headers; } }
package com.panxg.api.weather.one; import lombok.Data; / * @author panxg * @date 2021年06月21日 10:13 */ @Data public class WeatherEntity {
/ 城市ID*/ private String cityid; /当前日期*/ private String date; /当前星期*/ private String week; /气象台更新时间*/ private String update_time; /城市名称*/ private String city; /城市英文名称*/ private String cityEn; /国家名称*/ private String country; /国家英文名称*/ private String countryEn; /天气情况*/ private String wea; /天气对应图标 xue、lei、shachen、wu、bingbao、yun、yu、yin、qing*/ private String wea_img; /实时温度*/ private String tem; /高温*/ private String tem1; /低温*/ private String tem2; /风向*/ private String win; /风力等级*/ private String win_speed; /风速*/ private String win_meter; /湿度*/ private String humidity; /能见度*/ private String visibility; /气压hPa*/ private String pressure; /空气质量*/ private String air; /空气质量*/ private String air_pm25; /空气质量等级*/ private String air_level; /空气质量描述*/ private String air_tips; /*/ private AlarmEntity alarm; } @Data class AlarmEntity{
/*/ private String alarm_type; /*/ private String alarm_level; /*/ private String alarm_content; }
package com.panxg.api.weather.one; import cn.hutool.core.text.UnicodeUtil; import cn.hutool.http.HttpUtil; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.util.HashMap; / * @author panxg * @date 2021年06月21日 10:42 */ @Service @Slf4j public class WeatherService {
/ * cityid、city和ip参数3选一提交,如果不传,默认返回当前ip城市天气,cityid优先级最高。 * @param cityid 城市ID 请参考 城市ID列表 * @param city 城市名称 不要带市和区; 如: 青岛、铁西 * @param ip IP地址 查询IP所在城市天气 * @return com.panxg.entity.WeatherEntity * @Author panxg / * @Date 2021/6/21 10:50 / / public WeatherEntity queryWeather(String cityid,String city,String ip) {
HashMap<String, Object> headers = WeatherConfig.getConfigMap(); headers.put("cityid", cityid); headers.put("city", city); headers.put("ip", ip); String unicodeBody = HttpUtil.createGet(WeatherConfig.URL).form(headers).execute().body(); String strBody = UnicodeUtil.toString(unicodeBody); JSONObject jsonObject = JSONUtil.parseObj(strBody); return jsonObject.toBean(WeatherEntity.class); } }
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/176339.html原文链接:https://javaforall.net
