代码及注释: package com.jxctdzkj.cloudplatform.util;
import com.alibaba.fastjson.JSONObject; import com.jxctdzkj.cloudplatform.bean.DeviceBean; import com.jxctdzkj.cloudplatform.bean.VillageBean; import com.jxctdzkj.cloudplatform.config.DateCommon; import org.apache.commons.lang3.StringUtils; import org.nutz.dao.Cnd; import org.nutz.dao.Dao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;
import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List;
/**
@author Sure
@date 2020/10/19 19:19 */ @Component @EnableScheduling public class TaskSchedu { @Autowired Dao dao; @Scheduled(fixedDelay = 1000 *10) public void testByTenantId() throws Exception {
List<VillageBean> list=dao.query(VillageBean.class, Cnd.where("del","=",0)); //for循环是我遍历数据用的,可以忽略 for (int i=0;i<list.size();i++){ String defURL = " 这里是指定的url,因为是政府平台我就不放了"; URL url = new URL(defURL); // 打开和URL之间的连接 HttpURLConnection con = (HttpURLConnection)url.openConnection(); con.setRequestMethod("POST");//请求post方式 con.setUseCaches(false); // Post请求不能使用缓存 con.setDoInput(true);// 设置是否从HttpURLConnection输入,默认值为 true con.setDoOutput(true);// 设置是否使用HttpURLConnection进行输出,默认值为 false //设置header内的参数 connection.setRequestProperty("健, "值"); con.setRequestProperty("Content-Type", "application/json"); con.setRequestProperty("isTree", "true"); con.setRequestProperty("isLastPage", "true"); JSONObject param = new JSONObject(); String[] data=dao.fetch(DeviceBean.class,Cnd.where("Ncode","=",list.get(i).getDeviceNumber())).getData().split("\\|"); //参数 param.put("monitorname", list.get(i).getName()); param.put("addr", list.get(i).getAddress()); param.put("longitude", list.get(i).getPosition().split(",")[0]); param.put("latitude", list.get(i).getPosition().split(",")[1]); param.put("linkman", list.get(i).getPrincipal()); param.put("phone", list.get(i).getTel()); System.out.println("data====="+data); //不同的数据长度,最大是6位 for (int j=0;j<data.length;j++){ if(j==0){ param.put("temperature",data[0]); } if (j==1){ param.put("humidity", data[1]); } if (j==2){ param.put("carbondioxide", data[2]);} if (j==3){ param.put("winddirection", data[3]);} if (j==4){ param.put("windspeed", data[4]); } if (j==5){ param.put("illuminance", data[5]); } } SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式 System.out.println(df.format(new Date()));// new Date()为获取当前系统时间 param.put("pushdate",df.format(new Date())); //设置body内的参数,put到JSONObject中 System.out.println(param); // 建立实际的连接 con.connect(); // 得到请求的输出流对象 OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream(),"UTF-8"); writer.write(param.toString()); writer.flush(); // 获取服务端响应,通过输入流来读取URL的响应 InputStream is = con.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuffer sbf = new StringBuffer(); String strRead = null; while ((strRead = reader.readLine()) != null) { sbf.append(strRead); sbf.append("\r\n"); } reader.close(); // 关闭连接 con.disconnect(); // 打印读到的响应结果 System.out.println("运行结束:"+sbf.toString()); }} }