我做了一个天气接口测试,来引入数据,解析出来再把数据插到数据库里面去。
HttpClient需要导入的jar包为
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>
//初始化
CloseableHttpClient httpClient=HttpClients.createDefault();
//天气接口,token为令牌,dailysteps表示需要7天的数据
String URL="https://api.caiyunapp.com/v2/*token*/121.6544,25.1552/forecast.json?dailysteps=7&hourlysteps=168";
//实例化httpGet,傳入URL参数
HttpGet httpGet=new HttpGet(URL);//HttpPost
// 设置请求报头,模拟Chrome浏览器
httpGet.addHeader("Accept", "application/json, text/javascript, */*; q=0.01");
// httpGet.addHeader("Accept-Encoding", "gzip,deflate,sdch");
httpGet.addHeader("Accept-Language", "zh-CN,zh;q=0.8");
//设置编码
httpGet.addHeader("Content-Type", "text/html; charset=UTF-8");
//让浏览器记住访问的浏览器,可不写
httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36");
// 发送GET请求
CloseableHttpResponse response=httpClient.execute(httpGet);
// 从HTTP响应中取出页面内容
HttpEntity myEntity= response.getEntity();
String resString=EntityUtils.toString(myEntity);
// 打印出页面的内容:
System.out.println(resString);
//关闭连接
response.close();
httpClient.close();`
JSON为解析出来的数据,然后通过JsonObject和JsonArray去遍历自己需要的数据.
下面是我需要的数据,daily这个级别的,但是我需要先遍历到result,才能找到daily,因为result包含了daily,hourly个daily是一个级别的。
,"result":{"hourly":
"daily":{"status":"ok","coldRisk":[{"index":"4","desc":"\u6781\u6613\u53d1","datetime":"2017-08-17"},{"index":"4","desc":"\u6781\u6613\u53d1","datetime":"2017-08-18"},{"index":"4","desc":"\u6781\u6613\u53d1","datetime":"2017-08-19"},{"index":"4","desc":"\u6781\u6613\u53d1","datetime":"2017-08-20"},{"index":"4","desc":"\u6781\u6613\u53d1","datetime":"2017-08-21"},{"index":"3","desc":"\u6613\u53d1","datetime":"2017-08-22"},{"index":"4","desc":"\u6781\u6613\u53d1","datetime":"2017-08-23"}],"temperature":[{"date":"2017-08-17","max":35.0,"avg":28.3,"min":27.0},{"date":"2017-08-18","max":35.0,"avg":30.73,"min":27.0},{"date":"2017-08-19","max":35.0,"avg":30.57,"min":27.0},{"date":"2017-08-20","max":35.0,"avg":30.47,"min":27.0},{"date":"2017-08-21","max":35.0,"avg":30.47,"min":27.0},{"date":"2017-08-22","max":34.0,"avg":30.02,"min":27.0},{"date":"2017-08-23","max":35.0,"avg":30.56,"min":27.0}],"skycon":[{"date":"2017-08-17","value":"CLEAR_NIGHT"},{"date":"2017-08-18","value":"CLEAR_DAY"},{"date":"2017-08-19","value":"PARTLY_CLOUDY_DAY"},{"date":"2017-08-20","value":"CLEAR_DAY"},{"date":"2017-08-21","value":"PARTLY_CLOUDY_DAY"},{"date":"2017-08-22","value":"PARTLY_CLOUDY_DAY"},{"date":"2017-08-23","value":"PARTLY_CLOUDY_DAY"}],"cloudrate":[{"date":"2017-08-17","max":0.81,"avg":0.02,"min":0.0},{"date":"2017-08-18","max":0.53,"avg":0.19,"min":0.0},{"date":"2017-08-19","max":0.44,"avg":0.27,"min":0.05},{"date":"2017-08-20","max":0.43,"avg":0.13,"min":0.0},{"date":"2017-08-21","max":0.93,"avg":0.41,"min":0.0},{"date":"2017-08-22","max":1.0,"avg":0.7,"min":0.02},{"date":"2017-08-23","max":0.99,"avg":0.35,"min":0.0}],"aqi":[{"date":"2017-08-17","max":17.0,"avg":16.0,"min":15.0},{"date":"2017-08-18","max":27.0,"avg":22.25,"min":15.0},{"date":"2017-08-19","max":29.0,"avg":23.79,"min":22.0},{"date":"2017-08-20","max":30.0,"avg":25.5,"min":23.0},{"date":"2017-08-21","max":26.0,"avg":21.5,"min":16.0},{"date":"2017-08-22","max":16.0,"avg":10.88,"min":7.0},{"date":"2017-08-23","max":10.0,"avg":7.42,"min":7.0}],"humidity":[{"date":"2017-08-17","max":0.84,"avg":0.79,"min":0.65},{"date":"2017-08-18","max":0.82,"avg":0.74,"min":0.61},{"date":"2017-08-19","max":0.84,"avg":0.75,"min":0.63},{"date":"2017-08-20","max":0.84,"avg":0.76,"min":0.63},{"date":"2017-08-21","max":0.88,"avg":0.76,"min":0.6},{"date":"2017-08-22","max":0.86,"avg":0.79,"min":0.69},{"date":"2017-08-23","max":0.78,"avg":0.7,"min":0.58}],"astro":[{"date":"2017-08-17","sunset":{"time":"18:26"},"sunrise":{"time":"05:27"}},{"date":"2017-08-18","sunset":{"time":"18:26"},"sunrise":{"time":"05:28"}},{"date":"2017-08-19","sunset":{"time":"18:25"},"sunrise":{"time":"05:28"}},{"date":"2017-08-20","sunset":{"time":"18:24"},"sunrise":{"time":"05:29"}},{"date":"2017-08-21","sunset":{"time":"18:23"},"sunrise":{"time":"05:29"}},{"date":"2017-08-22","sunset":{"time":"18:22"},"sunrise":{"time":"05:30"}},{"date":"2017-08-23","sunset":{"time":"18:21"},"sunrise":{"time":"05:30"}}],"ultraviolet":[{"index":"4","desc":"\u5f3a","datetime":"2017-08-17"},{"index":"4","desc":"\u5f3a","datetime":"2017-08-18"},{"index":"4","desc":"\u5f3a","datetime":"2017-08-19"},{"index":"5","desc":"\u5f88\u5f3a","datetime":"2017-08-20"},{"index":"1","desc":"\u6700\u5f31","datetime":"2017-08-21"},{"index":"1","desc":"\u6700\u5f31","datetime":"2017-08-22"},{"index":"4","desc":"\u5f3a","datetime":"2017-08-23"}],"pm25":[{"date":"2017-08-17","max":9.0,"avg":9.0,"min":9.0},{"date":"2017-08-18","max":18.0,"avg":14.42,"min":9.0},{"date":"2017-08-19","max":19.0,"avg":15.54,"min":14.0},{"date":"2017-08-20","max":20.0,"avg":16.79,"min":15.0},{"date":"2017-08-21","max":17.0,"avg":13.88,"min":10.0},{"date":"2017-08-22","max":10.0,"avg":5.96,"min":3.0},{"date":"2017-08-23","max":5.0,"avg":3.29,"min":3.0}],"dressing":[{"index":"2","desc":"\u5f88\u70ed","datetime":"2017-08-17"},{"index":"2","desc":"\u5f88\u70ed","datetime":"2017-08-18"},{"index":"2","desc":"\u5f88\u70ed","datetime":"2017-08-19"},{"index":"2","desc":"\u5f88\u70ed","datetime":"2017-08-20"},{"index":"2","desc":"\u5f88\u70ed","datetime":"2017-08-21"},{"index":"3","desc":"\u70ed","datetime":"2017-08-22"},{"index":"3","desc":"\u70ed","datetime":"2017-08-23"}],"carWashing":[{"index":"1","desc":"\u9002\u5b9c","datetime":"2017-08-17"},{"index":"1","desc":"\u9002\u5b9c","datetime":"2017-08-18"},{"index":"1","desc":"\u9002\u5b9c","datetime":"2017-08-19"},{"index":"1","desc":"\u9002\u5b9c","datetime":"2017-08-20"},{"index":"1","desc":"\u9002\u5b9c","datetime":"2017-08-21"},{"index":"3","desc":"\u8f83\u4e0d\u9002\u5b9c","datetime":"2017-08-22"},{"index":"1","desc":"\u9002\u5b9c","datetime":"2017-08-23"}],"precipitation":[{"date":"2017-08-17","max":0.1635,"avg":0.0,"min":0.0},{"date":"2017-08-18","max":0.0,"avg":0.0,"min":0.0},{"date":"2017-08-19","max":0.0,"avg":0.0,"min":0.0},{"date":"2017-08-20","max":0.0,"avg":0.0,"min":0.0},{"date":"2017-08-21","max":0.0,"avg":0.0,"min":0.0},{"date":"2017-08-22","max":0.1173,"avg":0.0213,"min":0.0},{"date":"2017-08-23","max":0.0,"avg":0.0,"min":0.0}],"wind":[{"date":"2017-08-17","max":{"direction":72.01,"speed":11.28},"avg":{"direction":101.38,"speed":6.9},"min":{"direction":352.8,"speed":1.9}},{"date":"2017-08-18","max":{"direction":96.4,"speed":12.55},"avg":{"direction":103.45,"speed":8.39},"min":{"direction":316.8,"speed":1.97}},{"date":"2017-08-19","max":{"direction":129.41,"speed":12.34},"avg":{"direction":127.41,"speed":8.61},"min":{"direction":301.27,"speed":2.65}},{"date":"2017-08-20","max":{"direction":131.38,"speed":14.73},"avg":{"direction":127.25,"speed":9.63},"min":{"direction":304.21,"speed":1.88}},{"date":"2017-08-21","max":{"direction":90.07,"speed":18.8},"avg":{"direction":106.07,"speed":13.3},"min":{"direction":118.8,"speed":5.99}},{"date":"2017-08-22","max":{"direction":117.9,"speed":29.12},"avg":{"direction":117.89,"speed":22.59},"min":{"direction":115.72,"speed":14.82}},{"date":"2017-08-23","max":{"direction":132.85,"speed":31.92},"avg":{"direction":129.64,"speed":26.6},"min":{"direction":135.53,"speed":20.56}}]},"primary":0},"server_time":1502979403,"api_status":"active","tzshift":28800,"api_version":"v2.2","unit":"metric","location":[25.1552,121.6544]}
最后把解析出来的数据保存到一个对象里面,再把数据保存到数据库里面去,用FOR循环遍历7次,保存7天的天气数据。项目最后会用一个定时器去调用我解析出来的json数据,定时的去更新数据。定时器还没写,先到这。