package com.tobacco.project.mvc.common;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPInputStream;
/**
* @date : 2023/12/15 14:05
*/
public class WeatherData {
//高德地图开发服务 https://console.amap.com/dev/index
public static String getWeatherByGaoDe(){
String targetUrl="https://restapi.amap.com/v3/weather/weatherInfo";
String city = "查询地址编码";
String key = "网站注册,使用自己的key";
try {
// 编码参数值
String encodedParam1 = URLEncoder.encode(city, StandardCharsets.UTF_8.toString());
String encodedParam2 = URLEncoder.encode(key, StandardCharsets.UTF_8.toString());
// 构建带有参数的URL
String queryString = "?city=" + encodedParam1 + "&key=" + encodedParam2;
URL url = new URL(targetUrl + queryString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.connect();
// 检查响应编码是否为Gzip
String encoding = connection.getContentEncoding();
InputStream inputStream = connection.getInputStream();
Reader reader;
if ("gzip".equalsIgnoreCase(encoding)) {
// 解压缩Gzip响应
inputStream = new GZIPInputStream(inputStream);
reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
} else {
reader = new BufferedReader(new InputStreamReader(inputStream));
}
// 读取响应数据并解析JSON
StringBuilder content = new StringBuilder();
char[] buffer = new char[1024];
int bytesRead;
while ((bytesRead = reader.read(buffer)) != -1) {
content.append(buffer, 0, bytesRead);
}
reader.close();
// 解析JSON数据
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(content.toString());
// 在这里可以对JSON数据进行处理或访问特定字段
return rootNode.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//和风天气开发服务 https://dev.qweather.com/
public static String getWeatherByHeFeng() {
String targetUrl = "https://devapi.qweather.com/v7/weather/now";
String location = "查询地址编码";
String key = "网站注册,使用自己的key";
try {
// 编码参数值
String encodedParam1 = URLEncoder.encode(location, StandardCharsets.UTF_8.toString());
String encodedParam2 = URLEncoder.encode(key, StandardCharsets.UTF_8.toString());
// 构建带有参数的URL
String queryString = "?location=" + encodedParam1 + "&key=" + encodedParam2;
URL url = new URL(targetUrl + queryString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.connect();
// 检查响应编码是否为Gzip
String encoding = connection.getContentEncoding();
InputStream inputStream = connection.getInputStream();
Reader reader;
if ("gzip".equalsIgnoreCase(encoding)) {
// 解压缩Gzip响应
inputStream = new GZIPInputStream(inputStream);
reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
} else {
reader = new BufferedReader(new InputStreamReader(inputStream));
}
// 读取响应数据并解析JSON
StringBuilder content = new StringBuilder();
char[] buffer = new char[1024];
int bytesRead;
while ((bytesRead = reader.read(buffer)) != -1) {
content.append(buffer, 0, bytesRead);
}
reader.close();
// 解析JSON数据
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(content.toString());
// 在这里可以对JSON数据进行处理或访问特定字段
return rootNode.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}