xiaowang 8 mesiacov pred
rodič
commit
879070b3f5

+ 64 - 59
src/main/java/com/example/mdd/service/CaiYunDownload.java

@@ -23,67 +23,72 @@ public class CaiYunDownload {
     public void download(Connection conn, String stationCode, String longitude, String latitude) {
         //彩云天气api接口
         String url = "https://api.caiyunapp.com/v2.6/SRQIFijX5MGtdmhe/" + longitude + "," + latitude + "/hourly?hourlysteps=24 ";
-        String body = HttpUtil.createGet(url).execute().charset("utf-8").body();
-        JSONObject jsonObject = JSONUtil.parseObj(body);
-        JSONObject result = JSONUtil.parseObj(jsonObject.get("result"));
-        JSONObject hourly = JSONUtil.parseObj(result.get("hourly"));
-        JSONArray visibility = JSONUtil.parseArray(hourly.get("visibility"));
-        JSONArray temperature = JSONUtil.parseArray(hourly.get("temperature"));
-        JSONArray pressure = JSONUtil.parseArray(hourly.get("pressure"));
-        JSONArray humidity = JSONUtil.parseArray(hourly.get("humidity"));
-        JSONArray dswrf = JSONUtil.parseArray(hourly.get("dswrf"));
-        JSONArray wind = JSONUtil.parseArray(hourly.get("wind"));
-        List<CaiYun> list = new ArrayList<>();
-        if (!visibility.isEmpty()) {
-            for (int i = 0; i < 24; i++) {
-                CaiYun caiYun = new CaiYun();
-                Map vis = (Map) visibility.get(i);
-                Map temp = (Map) temperature.get(i);
-                Map pre = (Map) pressure.get(i);
-                Map hum = (Map) humidity.get(i);
-                Map ds = (Map) dswrf.get(i);
-                Map w = (Map) wind.get(i);
-                caiYun.setVisibility(vis.get("value").toString());
-                caiYun.setDatetime(vis.get("datetime").toString());
-                caiYun.setTemperature(temp.get("value").toString());
-                caiYun.setPressure(pre.get("value").toString());
-                caiYun.setHumidity(hum.get("value").toString());
-                caiYun.setDswrf(ds.get("value").toString());
-                caiYun.setDirection(w.get("direction").toString());
-                caiYun.setSpeed(new BigDecimal(String.valueOf(w.get("speed"))).divide(new BigDecimal("3.6"), 2, RoundingMode.HALF_UP).toString());
-                list.add(caiYun);
+        try {
+            String body = HttpUtil.createGet(url).execute().charset("utf-8").body();
+            JSONObject jsonObject = JSONUtil.parseObj(body);
+            JSONObject result = JSONUtil.parseObj(jsonObject.get("result"));
+            JSONObject hourly = JSONUtil.parseObj(result.get("hourly"));
+            JSONArray visibility = JSONUtil.parseArray(hourly.get("visibility"));
+            JSONArray temperature = JSONUtil.parseArray(hourly.get("temperature"));
+            JSONArray pressure = JSONUtil.parseArray(hourly.get("pressure"));
+            JSONArray humidity = JSONUtil.parseArray(hourly.get("humidity"));
+            JSONArray dswrf = JSONUtil.parseArray(hourly.get("dswrf"));
+            JSONArray wind = JSONUtil.parseArray(hourly.get("wind"));
+            List<CaiYun> list = new ArrayList<>();
+            if (!visibility.isEmpty()) {
+                for (int i = 0; i < 24; i++) {
+                    CaiYun caiYun = new CaiYun();
+                    Map vis = (Map) visibility.get(i);
+                    Map temp = (Map) temperature.get(i);
+                    Map pre = (Map) pressure.get(i);
+                    Map hum = (Map) humidity.get(i);
+                    Map ds = (Map) dswrf.get(i);
+                    Map w = (Map) wind.get(i);
+                    caiYun.setVisibility(vis.get("value").toString());
+                    caiYun.setDatetime(vis.get("datetime").toString());
+                    caiYun.setTemperature(temp.get("value").toString());
+                    caiYun.setPressure(pre.get("value").toString());
+                    caiYun.setHumidity(hum.get("value").toString());
+                    caiYun.setDswrf(ds.get("value").toString());
+                    caiYun.setDirection(w.get("direction").toString());
+                    caiYun.setSpeed(new BigDecimal(String.valueOf(w.get("speed"))).divide(new BigDecimal("3.6"), 2, RoundingMode.HALF_UP).toString());
+                    list.add(caiYun);
+                }
             }
-        }
-        if (!list.isEmpty()) {
-            String tableSql = "INSERT ignore INTO `caiyun` ( `stationCode`, `longitude`, `latitude`, `datetime`, `temperature`, `pressure`, `humidity`, `direction`, `speed`, `dswrf`, `visibility`) " +
-                    "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
-            try {
-                PreparedStatement pstmt = conn.prepareStatement(tableSql);
-                conn.setAutoCommit(false);
-                log.info("彩云天气:共{}条记录", list.size());
-                list.forEach(item -> {
-                    try {
-                        pstmt.setString(1, stationCode);
-                        pstmt.setString(2, longitude);
-                        pstmt.setString(3, latitude);
-                        pstmt.setString(4, item.getDatetime());
-                        pstmt.setString(5, item.getTemperature());
-                        pstmt.setString(6, item.getPressure());
-                        pstmt.setString(7, item.getHumidity());
-                        pstmt.setString(8, item.getDirection());
-                        pstmt.setString(9, item.getSpeed());
-                        pstmt.setString(10, item.getDswrf());
-                        pstmt.setString(11, item.getVisibility());
-                        pstmt.addBatch();
-                    } catch (SQLException e) {
-                        throw new RuntimeException(e);
-                    }
-                });
-                pstmt.executeBatch();
-                conn.commit();
-            } catch (Exception e) {
-                throw new RuntimeException(e);
+            if (!list.isEmpty()) {
+                String tableSql = "INSERT ignore INTO `caiyun` ( `stationCode`, `longitude`, `latitude`, `datetime`, `temperature`, `pressure`, `humidity`, `direction`, `speed`, `dswrf`, `visibility`) " +
+                        "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
+                try {
+                    PreparedStatement pstmt = conn.prepareStatement(tableSql);
+                    conn.setAutoCommit(false);
+                    log.info("彩云天气:共{}条记录", list.size());
+                    list.forEach(item -> {
+                        try {
+                            pstmt.setString(1, stationCode);
+                            pstmt.setString(2, longitude);
+                            pstmt.setString(3, latitude);
+                            pstmt.setString(4, item.getDatetime());
+                            pstmt.setString(5, item.getTemperature());
+                            pstmt.setString(6, item.getPressure());
+                            pstmt.setString(7, item.getHumidity());
+                            pstmt.setString(8, item.getDirection());
+                            pstmt.setString(9, item.getSpeed());
+                            pstmt.setString(10, item.getDswrf());
+                            pstmt.setString(11, item.getVisibility());
+                            pstmt.addBatch();
+                        } catch (SQLException e) {
+                            throw new RuntimeException(e);
+                        }
+                    });
+                    pstmt.executeBatch();
+                    conn.commit();
+                } catch (Exception e) {
+                    throw new RuntimeException(e);
+                }
             }
+        } catch (RuntimeException e) {
+//            throw new RuntimeException(e);
+            log.error("场站编号:{} 彩云天气数据下载失败JSON格式化错误", stationCode);
         }
         log.info("场站编号:{} 彩云天气数据下载完成", stationCode);
     }