scala獲取天氣介面
阿新 • • 發佈:2018-12-09
scala丟擲的介面
import com.cphe.base.DateHandle import com.cphe.util.SqlStringUtils import com.cphejava.jdbc.JDBCUtil import com.cphejava.util.WeatherForecast /** * Created by Administrator on 2018/9/11. */ object WeatherData extends DateHandle{ def main (args: Array[String]) { run() } def run()={ //刪除本次冗餘資料 val sql=SqlStringUtils.getRealSql("sql/score.xml","deleteWeather",curTime) println(sql) JDBCUtil.getInstance().executeUpdate(sql) val t = Array("city1","city2","city3","city4","city5","city6") // 第一種 for (x <- t) { WeatherForecast.run(x) } } }
java程式碼呼叫介面
package com.cphejava.util; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.apache.http.HttpResponse; import org.apache.http.util.EntityUtils; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import static com.pns.cphejava.util.OracleUtil.batch; /** * Created by Administrator on 2018/9/11. */ public class WeatherForecast { public static void run(String city) { String host = "https://saweather.market.alicloudapi.com"; String path = "/spot-to-weather"; String method = "GET"; String appcode = "2508e43e9ac54420a6163c2c07fa380b"; Map<String, String> headers = new HashMap<String, String>(); //最後在header中的格式(中間是英文空格)為Authorization:APPCODE 83359fd73fe94948385f570e3c139105 headers.put("Authorization", "APPCODE " + appcode); Map<String, String> querys = new HashMap<String, String>(); querys.put("area", city); querys.put("need3HourForcast", "0"); querys.put("needAlarm", "0"); querys.put("needHourData", "0"); querys.put("needIndex", "0"); querys.put("needMoreDay", "1"); try { /** * 重要提示如下: * HttpUtils請從 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java * 下載 * * 相應的依賴請參照 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml */ HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys); System.out.println(response.toString()); //獲取response的body String str= EntityUtils.toString(response.getEntity()); List<HashMap<String,Object>> list=testJSONStrToJSONArray(str); boolean result=write2DB(list); } catch (Exception e) { e.printStackTrace(); } } /** * 處理json串 * @param JSON_ARRAY_STR * @return */ public static List<HashMap<String,Object>> testJSONStrToJSONArray(String JSON_ARRAY_STR){ List<HashMap<String,Object>> list=new ArrayList<HashMap<String,Object>>(); JSONObject json= JSON.parseObject(JSON_ARRAY_STR); if("0".equals(json.getString("showapi_res_code"))){ JSONObject body= JSON.parseObject(json.getString("showapi_res_body")); JSONObject city= JSON.parseObject(body.getString("cityInfo")); for (int i=1;i<=7;i++){ HashMap<String, Object> map = new HashMap<String, Object>(); map.put("WRITE_TIME",body.getString("time").substring(0,8)); map.put("CITY",city.getString("c3")); JSONObject weather= JSON.parseObject(body.getString("f"+i)); map.put("DAY_WEATHER",weather.getString("day_weather")); map.put("DAY_WEATHER_CODE",weather.getString("day_weather_code")); map.put("DAY_AIR_TEMPERATURE",weather.getString("day_air_temperature")); map.put("NIGHT_AIR_TEMPERATURE",weather.getString("night_air_temperature")); map.put("DAT_TIME",weather.getString("day")); list.add(map); } } return list; } public static boolean write2DB ( List<HashMap<String,Object>> list){ String sql="insert into y_future_weather(write_time,city,day_weather,day_weather_code,night_air_temperature,day_air_temperature,dat_time)" + "values(to_date(?,'yyyymmdd'),?,?,?,?,?,to_date(?,'yyyymmdd'))"; int count = list.size(); // 插入記錄的數目 Object[][] params = new Object[count][]; for (int i = 0; i < count; i++) // 將每條記錄的資料插入陣列 params[i] = new Object[] { list.get(i).get("WRITE_TIME"), list.get(i).get("CITY"), list.get(i).get("DAY_WEATHER"), list.get(i).get("DAY_WEATHER_CODE"), list.get(i).get("NIGHT_AIR_TEMPERATURE"), list.get(i).get("DAY_AIR_TEMPERATURE"), list.get(i).get("DAT_TIME")}; try { batch(params, sql); return true; } catch (SQLException e) { e.printStackTrace(); return false; } } }
資料庫操作工具類
package com.cphejava.util; import org.apache.commons.dbutils.QueryRunner; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; /** * Created by Administrator on 2018/9/11. */ public class OracleUtil { private static final String JDBC_PATH="jdbc.properties"; private static final String DRIVER_CLASS_NAME = PropertyUtil.getProperty(JDBC_PATH, "jdbc.oracle.Driver"); private static final String JDBC_URL =PropertyUtil.getProperty(JDBC_PATH, "jdbc.oracle.url"); private static final String USER = PropertyUtil.getProperty(JDBC_PATH, "jdbc.oracle.username"); private static final String PASSWORD = PropertyUtil.getProperty(JDBC_PATH, "jdbc.oracle.password"); public static void main(String[] args) { int count = 10; // 插入記錄的數目 Object[][] params = new Object[count][]; String sql = "INSERT INTO TABLE_NAME VALUES (?,?,?)"; for (int i = 0; i < count; i++) // 將每條記錄的資料插入陣列 params[i] = new Object[] { "", "", "" }; try { batch(params,sql); } catch (SQLException e) { e.printStackTrace(); } } public static Connection getConnection() { Connection conn = null; try { Class.forName(DRIVER_CLASS_NAME); conn = DriverManager.getConnection(JDBC_URL, USER, PASSWORD); return conn; } catch (Exception e) { return null; } } public static void batch(Object[][] params,String sql) throws SQLException { QueryRunner queryRunner = new QueryRunner(true); queryRunner.batch(getConnection(), sql, params); } }
Maven依賴
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-dbutils/commons-dbutils -->
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.6</version>
</dependency>