//引用albaba的fastjson包
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
System.out.println("更新");
try {
// 连接中央气象台的API
String city= URLEncoder.encode("重庆","GBK");
URL url = new URL("http://apis.juhe.cn/simpleWeather/query?city="+city+"&key=d33cfd58c2513228a9f9e4f713c51024");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置请求方式
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept-Charset", "GBK");
System.out.println(connection.toString());
//setRequestProperty("Accept-Charset", "UTF-8")
// URLConnection connectionData = url.openConnection();
// connectionData.setConnectTimeout(1000);
//获取天气信息
// String url="http://apis.juhe.cn/simpleWeather/query?city=重庆&key=d33cfd58c2513228a9f9e4f713c51024";
// HttpURLConnection conn=(HttpURLConnection) url.offsetByCodePoints();
// System.out.println("url="+url);
// JSONObject result = HttpUtils.sendGet(url);
// conn.setRequestProperty("Accept-Charset", "UTF-8");
String datas=connection.toString();
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
Map<String, Object> map = new HashMap<String, Object>();
// BufferedReader br = new BufferedReader(new InputStreamReader(
// connectionData.toString()=, "UTF-8"));
StringBuilder sb = new StringBuilder();
// String line = br.readLine();
// while ((line = br.readLine()) != null)
// System.out.println("更新1");
// sb.append(line);
// if((line = br.readLine()) != null){
// System.out.println("更新1");
// sb.append(line);
// }
System.out.println("更新2");
System.out.println("datas:"+datas);
JSONObject jsonData = JSONObject.parseObject(datas);
System.out.println("jsonData:"+jsonData);
JSONObject info = jsonData.getJSONObject("result");
System.out.println("info:"+info);
JSONObject jsonData1 = JSONObject.parseObject(info.getString("realtime").toString());
System.out.println("jsonData1:"+jsonData1);
map.put("city", "重庆");// 城市
map.put("humidity",jsonData1.getString("humidity"));// 湿度
map.put("direct",jsonData1.getString("direct"));// 风向
map.put("power",jsonData1.getString("power"));// 风力
map.put("info",jsonData1.getString("info"));// 天气
map.put("wid",jsonData1.getString("wid"));// 天气类型
JSONArray jsonData2= JSONArray.parseArray(info.getString("future").toString());
System.out.println("更新3");
for (int i = 0; i < 1; i++) {
System.out.println("更新4");
JSONObject jsonObj = jsonData2.getJSONObject(i);
//获取日期
String time = jsonObj.getString("date").toString();
//调用星期处理方法strToDate
String week = strToDate(time);
map.put("week",week);//星期
map.put("date",jsonObj.getString("date"));// 日期
map.put("temperature",jsonObj.getString("temperature"));// 温度
map.put("weather",jsonObj.getString("weather"));// 天气
}
list.add(map);
//初始化对象
System.out.println("更新5");
SWeather weatherinfo=new SWeather();
for (int k = 0; k < list.size(); k++) {
System.out.println("更新6");
weatherinfo.setWeatherId("1");
weatherinfo.setTemperature(list.get(k).get("temperature").toString());//温度
weatherinfo.setInfo(list.get(k).get("info").toString());//天气
weatherinfo.setPower(list.get(k).get("power").toString());//风力
weatherinfo.setHumidity(list.get(k).get("humidity").toString()+"%RH");//湿度
weatherinfo.setDirect(list.get(k).get("direct").toString());//风向
weatherinfo.setWid(list.get(k).get("wid").toString());//天气类型
weatherinfo.setRefreshTime(new Date());//刷新时间
//保存写入信息
weatherRepository.save(weatherinfo);
}
} catch (Exception e) {
System.out.println(e);
System.out.println("请求超时,加载信息出错或天气数据超过每日可允许请求次数!");
}
/**
* 时间获得星期
* @param strDate
* @return
* @throws ParseException
*/
public static String strToDate(String strDate) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(formatter.parse(strDate));
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
String week = sdf.format(c.getTime());
return week;
}