申请和风天气key以及获取所在地Location
和风天气接口API:https://dev.qweather.com/docs/api/
1、如何请求
public class WeatherApi2 {
public static void main(String[] args) throws Exception {
String url = "https://devapi.qweather.com/v7/weather/7d?location=城市代码&key=自己申请的key";
RestTemplate restTemplate = new RestTemplate();
byte[] oResult = restTemplate.exchange(url, HttpMethod.GET, null, byte[].class).getBody();
String unGZipResult = unGZip(oResult);
System.out.println(unGZipResult);
}
public static String unGZip(byte[] oResult) throws Exception {
try (InputStream inputStream = new ByteArrayInputStream(oResult);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream)) {
byte[] buf = new byte[4096];
int len = -1;
while ((len = gzipInputStream.read(buf, 0, buf.length)) != -1) {
byteArrayOutputStream.write(buf, 0, len);
}
return new String(byteArrayOutputStream.toByteArray