首页 > 其他分享 >doPost请求

doPost请求

时间:2023-12-28 15:48:34浏览次数:32  
标签:status positiveActiveDegreeValue energyConsumerHistory doPost json new BigDecima

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() + " ,执行已完成 ****************************");
}
}







标签:status,positiveActiveDegreeValue,energyConsumerHistory,doPost,json,new,BigDecima
From: https://www.cnblogs.com/sensenh/p/17932822.html

相关文章

  • c#语法JWT请求接口
    NuGet下载System.IdentityModel.Tokens.Jwt包下面是获取token,需要引用对应的jwtusingSystem.IdentityModel.Tokens.Jwt;usingMicrosoft.IdentityModel.Tokens;privatevoidbutton1_Click(objectsender,EventArgse){stringsecretId="a1bc3900b......
  • 超时控制:Go语言下的网络请求与时间赛跑
    开场白:在互联网的世界里,我们经常要与各种API打交道。有时,这些API可能会因为各种原因而变得“慢条斯理”,这时,超时控制就显得尤为重要了。今天,我们就来聊聊如何在Go语言中实现HTTP请求的超时控制,与时间赛跑!知识点一:了解超时控制的必要性想象一下,当你正在等待一个重要的API响应时,如果......
  • 跨域请求:Go语言下的“通天大道”
    开场白:嘿,各位Go语言的爱好者们,你们是否曾经遇到过这样的困扰:当你的Go应用试图与另一个域的API进行交流时,突然跳出一个“未允许的跨域请求”的警告?别担心,今天,我们将一起在这条“通天大道”上漫步,解决跨域的困扰!知识点一:何为跨域问题?简单来说,当你在浏览器中访问一个网页,该网页试图从......
  • 处理HTTP请求的gzip压缩
    开场白:在互联网的浩瀚海洋中,数据传输是不可或缺的一环。为了提高传输效率,许多服务采用gzip压缩技术对数据进行压缩。今天,我们将深入探讨如何在Go语言中优雅地处理HTTP请求的gzip压缩。知识点一:gzip压缩的原理Gzip压缩是一种广泛使用的压缩算法,通过将数据中的重复模式和冗余信息进行......
  • 前端发送请求的时候 Content-Type 内容分类, django后端分别怎么接收
    前端发送请求时,常见的Content-Type内容类型包括:application/x-www-form-urlencoded这是最常见的内容类型,用于发送键值对形式的数据。数据被编码为URL查询字符串格式。Django后端可以通过request.POST来接收这些参数。multipart/form-data通常用于文件上传的表......
  • 海量请求的应对方法
    1.网络时延工具:ping、tcpdump、2.nginx查看运行状态 参数说明:3.ab测试 说明:  4.查看系统的运行状况-top命令参数说明:5.查看磁盘运行情况-vmstat和sar命令   6.查看网络链接状况-netstat   7.其他工具-Nmon可配合NmonAnalyser根......
  • Jmeter —— 接口之间关联调用(获取上一个接口的返回值作为下一个接口的请求参数)
    正则表达式:具体如何操作:1.草稿保存,此请求的响应数据的id为发布总结的请求参数draft_id2.草稿保存的响应数据3.在草稿保存的请求中,添加后置处理器-正则表达式提取器,提取响应数据的id信息4.发布总结请求中,填写请求参数5.最终,可正常发布最后如果你想学习软件测......
  • PMP-6.1 监控项目工作--工作绩效报告、变更请求
    #########################################在规划过程组中,制定项目管理计划属于项目整合管理。在执行过程组中,指导与管理项目工作和管理项目知识属于项目整合管理。监控项目工作和实施整体变更控制都属于项目整合管理领域。项目整合管理领域是对项目整体的控制的领域。#####......
  • 接口请求响应码415报错信息UNSUPPORTED_MEDIA_TYPE问题记录
    场景描述开发了一个Post类型的接口,在本地用postman测试是没有问题。前端调用返回415UNSUPPORTED_MEDIA_TYPE。而415UnsupportedMediaType是一种HTTP协议的错误状态代码,表示服务器由于不支持其有效载荷的格式,从而拒绝接受客户端的请求后面定位问题发现,后端接口请求参数使用@......
  • [Nginx] API请求体过大时,报:Nginx 413 Request Entity Too Large [转载]
    1问题描述此问题(通过方法1)已解决,但整体的解决思路,参考自网友,故著名为【转载】文章昨晚压测时,通过API请求10000个设备的若干信号(5个)状态时,Postman返回Nginx的如下错误API整个请求有1.37MB413RequestEntityTooLarge2问题分析通过字面意思,即可知悉:nginx的cl......