doPost请求方法:
public static JSONObject doPost4(String url, JSONObject json) {
int rTimeOut = 10000;
int sTimeOut = 10000;
//设置超时时间10秒
BasicHttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, rTimeOut);
HttpConnectionParams.setSoTimeout(httpParams, sTimeOut);
DefaultHttpClient client = new DefaultHttpClient(httpParams);
// DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
if (!forward.isEmpty()) {
post = new HttpPost(forward);
}
JSONObject response = null;
try {
StringEntity s = new StringEntity(json.toString());
s.setContentEncoding("UTF-8");
s.setContentType("application/json");//发送json数据需要设置contentType
post.setEntity(s);
if (!forward.isEmpty()){
post.addHeader("url", url);
}
HttpResponse res = client.execute(post);
if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
// org.apache.http.HttpEntity entity = res.getEntity();
String result = EntityUtils.toString(res.getEntity());// 返回json格式:
response = JSONObject.fromObject(result);
}
} catch (Exception e) {
log.error("RestUtil.doPost4 error:{}",e.getMessage());
}
return response;
}
eg:
@Scheduled(cron = "${energyConsumer.history}")
@SchedulerLock(name = "EnergyConsumerHistory")
public void addEnergyConsumerHistory() {
// logger.info("获取数据");
energyService.queryECAmmeterStatus();
}
@Override
public void queryECAmmeterStatus() {
List<StationSensorInfo> list = stationSensorInfoDao.findType1("电表");
int count = 0;
for (StationSensorInfo sensorInfo : list) {
String url = sensorInfo.getUrl();
String secretKey = sensorInfo.getSecretKey();
String pubKey = sensorInfo.getPubKey();
String signSecret = sensorInfo.getSignSecret();
String appId = sensorInfo.getAppId();
String serviceCode = sensorInfo.getServiceCode();
Station station = stationDao.findByStationTelecode(sensorInfo.getStationCode());
List<EnergyStaVO> devices = deviceDao.queryECAmmeterStatus(station.getId());
System.out.println("**************************** 第 " + (++count) + " 次开始获取数据... ****************************");
if (devices !=null && devices.size()>0){
Map<String, EnergyStaVO> map = new HashMap<>();
JSONArray paramArray = new JSONArray();
for (EnergyStaVO device: devices){
net.sf.json.JSONObject paramObj = new net.sf.json.JSONObject();
paramObj.put("deviceId", device.getDeviceId());
paramArray.add(paramObj);
map.put(device.getDeviceId(),device);
}
// JSON json = (JSON) JSON.toJSON(paramArray);
String appSecret = pubKey;
// try {
// appSecret = RSAEncryptAccessSystem.encrypt(secretKey, pubKey);
// } catch (Exception e) {
// e.printStackTrace();
// }
// String sign = EncryptUtilAccessSystem.MD5(json, signSecret);
net.sf.json.JSONObject jsonObject = new net.sf.json.JSONObject();
jsonObject.put("serviceCode", serviceCode);
// jsonObject.put("bureauCode", sensorInfo.getBureauCode());
// jsonObject.put("deptCode", sensorInfo.getStationCode());
jsonObject.put("appId", appId);
// jsonObject.put("sign", sign);
jsonObject.put("appSecret", appSecret);
jsonObject.put("bas_ip", station.getBasIp());//获取对应车站服务器ip
jsonObject.put("params", paramArray);
System.out.println(jsonObject);
System.out.println("**************************** 正在获取能耗数据... ****************************");
net.sf.json.JSONObject jsonObject1 = RestUtil.doPost4(url, jsonObject);
System.out.println("**************************** 获取数据完毕... ****************************");
System.out.println(jsonObject1);
if (jsonObject1 != null && "00000".equals(jsonObject1.getString("code"))) {
net.sf.json.JSONObject data = jsonObject1.getJSONObject("data");
String stationCode = data.getString("stationCode");
net.sf.json.JSONArray jsonObjString = data.getJSONArray("info");
for (Object o : jsonObjString) {
JSONObject jsonObject2 = JSONObject.parseObject(o.toString());
QueryECAmmeterStatus status = JSON.toJavaObject(jsonObject2, QueryECAmmeterStatus.class);
EnergyConsumerHistory energyConsumerHistory = new EnergyConsumerHistory();
EnergyConsumer energyConsumer = new EnergyConsumer();
if (map.get(status.getDeviceId())!=null){
energyConsumer.setId(status.getDeviceId());
energyConsumer.setStationCode(stationCode);
energyConsumer.setDeviceType(map.get(status.getDeviceId()).getName().substring(0,2));
energyConsumer.setForeignCode(status.getDeviceId());
String positiveActiveDegreeValue = status.getPositiveActiveDegreeValue();
if(!StringUtils.isEmpty(positiveActiveDegreeValue) && isNumeric(positiveActiveDegreeValue)) {
energyConsumer.setEnergyValue(new BigDecimal(positiveActiveDegreeValue));
}
//energyConsumer.setEnergyValue(new BigDecimal(org.springframework.util.StringUtils.isEmpty(positiveActiveDegreeValue) || "+inf".equals(positiveActiveDegreeValue) ? "0" : positiveActiveDegreeValue));
energyConsumer.setUpdateTime(status.getUpdateTime());
energyConsumerDao.save(energyConsumer);
energyConsumerHistory.setStationCode(stationCode);
energyConsumerHistory.setDeviceType(map.get(status.getDeviceId()).getName().substring(0,2));
energyConsumerHistory.setSregion(map.get(status.getDeviceId()).getAddress());
energyConsumerHistory.setForeignCode(status.getDeviceId());
// energyConsumerHistory.setUa(new BigDecimal(status.getAVoltageValue()));
// energyConsumerHistory.setUb(new BigDecimal(status.getBVoltageValue()));
// energyConsumerHistory.setUc(new BigDecimal(status.getCVoltageValue()));
// energyConsumerHistory.setIa(new BigDecimal(status.getAElectricityValue()));
// energyConsumerHistory.setIa(new BigDecimal(status.getBElectricityValue()));
// energyConsumerHistory.setIa(new BigDecimal(status.getCElectricityValue()));
// energyConsumerHistory.setReactivePower(new BigDecimal(status.getSysReactivepowerValue()));
// energyConsumerHistory.setKw(new BigDecimal(status.getSysActivepowerValue()));
// energyConsumerHistory.setKVARH(new BigDecimal(status.getPositiveReactiveDegreeValue()));
if(!StringUtils.isEmpty(positiveActiveDegreeValue) && isNumeric(positiveActiveDegreeValue)) {
energyConsumerHistory.setEnergyValue(new BigDecimal(positiveActiveDegreeValue));
}
//energyConsumerHistory.setEnergyValue(new BigDecimal(org.springframework.util.StringUtils.isEmpty(positiveActiveDegreeValue) || "+inf".equals(positiveActiveDegreeValue) ? "0" : positiveActiveDegreeValue));
// energyConsumerHistory.setPowerFactor(new BigDecimal(status.getTotalActivePowerValue()));
energyConsumerHistory.setUpdateTime(status.getUpdateTime());
energyConsumerHistory.setCreateTime(new Date());
energyConsumerHistoryDao.save(energyConsumerHistory);
}
}
}
}
System.out.println("**************************** 当前车站名称: " + station.getStationName() + " ,执行已完成 ****************************");
}
}