|
|
@@ -0,0 +1,104 @@
|
|
|
+package com.yunfeiyun.agmp.common.utils.weather_backup;
|
|
|
+
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class WeatherUtil {
|
|
|
+
|
|
|
+ public static final String WEATHER_API = "https://v0.yiketianqi.com/api?";
|
|
|
+
|
|
|
+ public static String getWeather(WeatherReqVo weatherReqVo) {
|
|
|
+ String path = appendRequestParam(weatherReqVo);
|
|
|
+ HttpURLConnection conn;
|
|
|
+ try {
|
|
|
+ URL url = new URL(path);
|
|
|
+ conn = (HttpURLConnection) url.openConnection();
|
|
|
+ conn.setRequestMethod("GET");
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("请求构造失败",e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try(InputStream in = conn.getInputStream();
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))){
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
+ String line;
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ result.append(line);
|
|
|
+ }
|
|
|
+ return result.toString();
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("请求失败",e);
|
|
|
+ return null;
|
|
|
+ }finally {
|
|
|
+ conn.disconnect();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getWeatherNearlySevenDays(WeatherReqVo weatherReqVo) {
|
|
|
+ return request(appendRequestParam(weatherReqVo));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String request(String path) {
|
|
|
+ HttpURLConnection conn;
|
|
|
+ try {
|
|
|
+ URL url = new URL(path);
|
|
|
+ conn = (HttpURLConnection) url.openConnection();
|
|
|
+ conn.setRequestMethod("GET");
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("请求构造失败",e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try(InputStream in = conn.getInputStream();
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))){
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
+ String line;
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ result.append(line);
|
|
|
+ }
|
|
|
+ return result.toString();
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("请求失败",e);
|
|
|
+ return null;
|
|
|
+ }finally {
|
|
|
+ conn.disconnect();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String appendRequestParam(Object... params){
|
|
|
+ StringBuilder requestParam = new StringBuilder();
|
|
|
+
|
|
|
+ for(Object o :params){
|
|
|
+ if(null == o){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Class<?> clazz = o.getClass();
|
|
|
+ Field[] fields = clazz.getDeclaredFields();
|
|
|
+ for (Field field : fields) {
|
|
|
+ try {
|
|
|
+ field.setAccessible(true);
|
|
|
+ String propertyName = field.getName();
|
|
|
+ Object propertyValue = field.get(o);
|
|
|
+ if(null != propertyValue){
|
|
|
+ requestParam.append("&").append(propertyName).append("=").append(propertyValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ;
|
|
|
+ return WEATHER_API+ requestParam.substring(1);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|