package com.nongyuanbao.server;
import java.io.IOException;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSON;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.neptune.core.project.AutoIdHelper;
import com.nongyuanbao.vo.SrtResultVO;
import com.nongyuanbao.weather.model.Report;
public class WeatherReportServer {
/**
* 一个小时爬30次数据
*/
@Before(Tx.class)
public void addWeatherreport(){
CloseableHttpClient httpCilent2 = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(5000) //设置连接超时时间
.setConnectionRequestTimeout(5000) // 设置请求超时时间
.setSocketTimeout(5000)
.setRedirectsEnabled(true)//默认允许自动重定向
.build();
HttpGet httpGet2 = new HttpGet("https://free-api.heweather.com/s6/weather/now?location=%E9%B2%81%E5%B1%B1&key=1d39bc6e725649dabebf1a3c1f27f4cc");
httpGet2.setConfig(requestConfig);
String srtResult = "";
try {
HttpResponse httpResponse = httpCilent2.execute(httpGet2);
if(httpResponse.getStatusLine().getStatusCode() == 200){
srtResult = EntityUtils.toString(httpResponse.getEntity());//获得返回的结果
//srtResult.substring(srtResult.indexOf(":"),srtResult.lastIndexOf("}"));
List<SrtResultVO> srtResultList = JSON.parseArray( srtResult.substring(srtResult.indexOf(":")+1,srtResult.lastIndexOf("}")), SrtResultVO.class);
System.out.println(srtResultList);
for(SrtResultVO vo : srtResultList){
Report weatherreport = new Report();
weatherreport.setId(AutoIdHelper.getId());
weatherreport.setCid(vo.getBasic().getCid());
weatherreport.setLocation(vo.getBasic().getLocation());
weatherreport.setCity(vo.getBasic().getParent_city());
weatherreport.setProvince(vo.getBasic().getAdmin_area());
weatherreport.setCnty(vo.getBasic().getCnty());
weatherreport.setLat(vo.getBasic().getLat());
weatherreport.setLon(vo.getBasic().getLon());
weatherreport.setTz(vo.getBasic().getTz());
weatherreport.setLoc(vo.getUpdate().getLoc());
weatherreport.setUtc(vo.getUpdate().getUtc());
weatherreport.setCloud(vo.getNow().getCloud());
weatherreport.setCondCode(vo.getNow().getCond_code());
weatherreport.setCondTxt(vo.getNow().getCond_txt());
weatherreport.setFl(vo.getNow().getFl());
weatherreport.setHum(vo.getNow().getHum());
weatherreport.setPcpn(vo.getNow().getPcpn());
weatherreport.setPres(vo.getNow().getPres());
weatherreport.setTmp(vo.getNow().getTmp());
weatherreport.setVis(vo.getNow().getVis());
weatherreport.setWindDeg(vo.getNow().getWind_deg());
weatherreport.setWindDir(vo.getNow().getWind_dir());
weatherreport.setWindSc(vo.getNow().getWind_sc());
weatherreport.setWindSpd(vo.getNow().getWind_spd());
weatherreport.setStatus(vo.getStatus());
System.out.println(weatherreport);
weatherreport.save();
}
}else if(httpResponse.getStatusLine().getStatusCode() == 400){
//..........
}else if(httpResponse.getStatusLine().getStatusCode() == 500){
//.............
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
httpCilent2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.nongyuanbao.task;
import com.jfinal.aop.Duang;
import com.nongyuanbao.server.WeatherReportServer;
public class WeatherReportTask implements Runnable {
@Override
public void run() {
WeatherReportServer weatherReportServer = Duang.duang(WeatherReportServer.class);
weatherReportServer.addWeatherreport();
}
}
cron4j=weatherReport
#demo.cron=* * * * *
#demo.class=
#demo.daemon=true
#demo.enable=true
#两åéè¿è¡ä¸æ¬¡
weatherReport.cron=*/2 * * * *
weatherReport.class=com.nongyuanbao.task.WeatherReportTask
weatherReport.daemon=true
weatherReport.eable=true
}