第一次写博客(如有侵权请通知我,立马删除)
获取天气数据 (根据天气接口返回的数据)
接口:心知天气
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);