|
@@ -7,6 +7,7 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStreamReader;
|
|
|
+import java.net.ConnectException;
|
|
|
import java.net.URL;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.HashMap;
|
|
@@ -17,6 +18,24 @@ public class YiKeYunApiUtils {
|
|
|
private static final String API_YIKEYUN_APPID = "97543277";
|
|
|
private static final String API_YIKEYUN_APPSECRET = "PXd7GE2j";
|
|
|
|
|
|
+ public static Map<String, String> getTodayWeatherByAdcodeTry(String adcode) {
|
|
|
+ for (int i = 0; i < 3; i++) {
|
|
|
+ try {
|
|
|
+ return getTodayWeatherByAdcode(adcode);
|
|
|
+ } catch (IOException e) {
|
|
|
+ // 重试
|
|
|
+ try {
|
|
|
+ Thread.sleep(500);
|
|
|
+ } catch (InterruptedException ex) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -25,29 +44,24 @@ public class YiKeYunApiUtils {
|
|
|
* @param adcode
|
|
|
* @return
|
|
|
*/
|
|
|
- public static Map<String, String> getTodayWeatherByAdcode(String adcode) {
|
|
|
+ public static Map<String, String> getTodayWeatherByAdcode(String adcode) throws IOException {
|
|
|
String getUrl = String.format("http://v1.yiketianqi.com/free/day?appid=%s&appsecret=%s&unescape=1&adcode=%s", API_YIKEYUN_APPID, API_YIKEYUN_APPSECRET, adcode);
|
|
|
Map<String, String> map = new HashMap<>();
|
|
|
- try {
|
|
|
- URL url = new URL(getUrl);
|
|
|
- BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
|
|
|
- String res;
|
|
|
- StringBuilder sb = new StringBuilder();
|
|
|
- while ((res = in.readLine()) != null) {
|
|
|
- sb.append(res.trim());
|
|
|
- }
|
|
|
- log.info("获取当天天气 ======= sb.toString():" + sb);
|
|
|
- JSONObject jsonData = JSONObject.parseObject(sb.toString());
|
|
|
- map.put("temp", jsonData.get("tem").toString());
|
|
|
- map.put("weather", jsonData.get("wea").toString());
|
|
|
- map.put("high", jsonData.get("tem_day").toString());
|
|
|
- map.put("low", jsonData.get("tem_night").toString());
|
|
|
- map.put("windLevel", jsonData.get("win_speed").toString());
|
|
|
- map.put("nums", jsonData.getInteger("nums").toString());
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- return null;
|
|
|
+ URL url = new URL(getUrl);
|
|
|
+ BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
|
|
|
+ String res;
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ while ((res = in.readLine()) != null) {
|
|
|
+ sb.append(res.trim());
|
|
|
}
|
|
|
+ log.info("获取当天天气 ======= sb.toString():" + sb);
|
|
|
+ JSONObject jsonData = JSONObject.parseObject(sb.toString());
|
|
|
+ map.put("temp", jsonData.get("tem").toString());
|
|
|
+ map.put("weather", jsonData.get("wea").toString());
|
|
|
+ map.put("high", jsonData.get("tem_day").toString());
|
|
|
+ map.put("low", jsonData.get("tem_night").toString());
|
|
|
+ map.put("windLevel", jsonData.get("win_speed").toString());
|
|
|
+ map.put("nums", jsonData.getInteger("nums").toString());
|
|
|
return map;
|
|
|
}
|
|
|
|
|
@@ -119,7 +133,7 @@ public class YiKeYunApiUtils {
|
|
|
});
|
|
|
|
|
|
|
|
|
- } catch (IOException e) {
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return null;
|
|
|
}
|