免费天气API接口
package com.panxg.api.weather.two;
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;
@Service
@Slf4j
public class SimpleWeather {
public static final String API_QUERY_URL = "http://apis.juhe.cn/simpleWeather/query";
public static final String API_LIFE_URL = "http://apis.juhe.cn/simpleWeather/life";
public static final String API_WIDS_URL = "http://apis.juhe.cn/simpleWeather/wids";
public static final String API_CITYLIST_URL = "http://apis.juhe.cn/simpleWeather/cityList";
public static final String API_KEY = "2d7652bf150fc43f87e854f5eff2c488";
public String querySimpleWeatherCityList() {
String result = "";
HashMap<String, Object> headers = new HashMap<>();
headers.put("key", API_KEY);
String body = HttpUtil.createGet(API_CITYLIST_URL).form(headers).execute().body();
JSONObject jsonObject = JSONUtil.parseObj(body);
if ("0".equals(jsonObject.getStr("error_code"))) {
result = jsonObject.getStr("result");
log.info("查询成功!"+result);
} else {
log.info("查询失败!"+jsonObject);
result = jsonObject.toString();
}
return result;
}
public String querySimpleWeatherWids() {
String result = "";
HashMap<String, Object> headers = new HashMap<>();
headers.put("key", API_KEY);
String body = HttpUtil.createGet(API_WIDS_URL).form(headers).execute().body();
JSONObject jsonObject = JSONUtil.parseObj(body);
if ("0".equals(jsonObject.getStr("error_code"))) {
result = jsonObject.getStr("result");
log.info("查询成功!"+result);
} else {
log.info("查询失败!"+jsonObject);
result = jsonObject.toString();
}
return result;
}
public String querySimpleWeatherLife(String cityName) {
String result = "";
HashMap<String, Object> headers = new HashMap<>();
headers.put("city", cityName);
headers.put("key", API_KEY);
String body = HttpUtil.createGet(API_LIFE_URL).form(headers).execute().body();
JSONObject jsonObject = JSONUtil.parseObj(body);
if ("0".equals(jsonObject.getStr("error_code"))) {
result = jsonObject.getStr("result");
log.info("查询成功!"+result);
} else {
log.info("查询失败!"+jsonObject);
result = jsonObject.toString();
}
return result;
}
public ResultEntity querySimpleWeatherQuery(String cityName) {
ResultEntity resultEntity=new ResultEntity();
HashMap<String, Object> headers = new HashMap<>();
headers.put("city", cityName);
headers.put("key", API_KEY);
String body = HttpUtil.createGet(API_QUERY_URL).form(headers).execute().body();
JSONObject jsonObject = JSONUtil.parseObj(body);
log.info("查询成功!" + jsonObject);
if ("0".equals(jsonObject.getStr("error_code"))) {
log.info("查询成功!");
Object result = jsonObject.get("result");
resultEntity = JSONUtil.parseObj(result).toBean(ResultEntity.class);
} else {
log.info("查询失败!"+jsonObject);
}
return resultEntity;
}
}
package com.panxg.api.weather.two;
import lombok.Data;
@Data
public class FutureEntity {
private String date;
private String temperature;
private String weather;
private WidEntity wid;
private String direct;
}
package com.panxg.api.weather.two;
import lombok.Data;
@Data
public class RealtimeEntity {
private String city;
private String info;
private String temperature;
private String humidity;
private String direct;
private String power;
private String aqi;
}
package com.panxg.api.weather.two;
import lombok.Data;
import java.util.List;
@Data
public class ResultEntity {
private String city;
private RealtimeEntity realtimeEntity;
private List<FutureEntity> future;
}
package com.panxg.api.weather.two;
import lombok.Data;
@Data
public class WidEntity {
private String day;
private String night;
}