獲取天氣資料 (根據天氣介面返回的資料)
阿新 • • 發佈:2018-12-12
第一次寫部落格(如有侵權請通知我,立馬刪除)
獲取天氣資料 (根據天氣介面返回的資料)
介面:心知天氣
ulr:https://api.seniverse.com/v3/pro/weather/grid/now.json?key=your_api_key&location=39.865927:116.359805
請求引數:
key:你的API金鑰
location:所查詢的位置
引數值範圍:
經緯度 例如:location=39.93:116.40(格式是 緯度:經度,英文冒號分隔)
unit
單位 (可選)
引數值範圍:
c 當引數為c時,溫度c、風速km/h、能見度km、氣壓mb
f 當引數為f時,溫度f、風速mph、能見度mile、氣壓inch
預設值:c
//請求介面的工具類
public class GetWeatherUtil {
public static String loadJSON(String LonAndLat) { String url="https://api.seniverse.com/v3/pro/weather/grid/now.json?key=dek53kmszetbxnoj&location="+LonAndLat; StringBuilder json = new StringBuilder(); try { URL oracle = new URL(url); URLConnection yc = oracle.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(),"UTF-8")); String inputLine = null; while ((inputLine = in.readLine()) != null) { json.append(inputLine); } in.close(); } catch (MalformedURLException e) { } catch (IOException e) { } return json.toString(); }
}
//這個功能沒有用任何框架,都是用的最基本的jdbc
//把獲取下來的json資料 一個個取出來 拼接sql語句實現增加功能
//每個小時的整點都請求一次(定時器用的是Timer)
TimerTask wetask = new TimerTask() {
@Override
public void run() {
new WriteRegionWeather().writeRegionWeather(); } }; Timer timer = new Timer(); Calendar currentTime = Calendar.getInstance(); currentTime.setTime(new Date()); int currentHour = currentTime.get(Calendar.HOUR); currentTime.set(Calendar.HOUR, currentHour + 1); currentTime.set(Calendar.MINUTE, 0); currentTime.set(Calendar.SECOND, 0); currentTime.set(Calendar.MILLISECOND, 0); Date NextHour = currentTime.getTime(); timer.scheduleAtFixedRate(wetask, NextHour, 1000*60*60);