客户端代码


/**
* 从接口获取数据
* @param url 服务器接口地址
* @param json 传入的参数 若获取全部,此项为空
* @return 返回查询到的数据
* @throws HttpException
* @throws IOException
*/
public JSONObject post(String url, String json) throws HttpException, IOException{
try {
CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url);
httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json;charset=utf-8");
StringEntity se = new StringEntity(json, Charset.forName("UTF-8"));
se.setContentType("text/json");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); httpPost.setEntity(se);
HttpResponse response = httpClient.execute(httpPost); if (response != null && response.getStatusLine().getStatusCode() == 200){
String result = EntityUtils.toString(response.getEntity(), "UTF-8");
JSONObject resultJsonObject = JSONObject.parseObject(result);
return resultJsonObject;
}
}catch (Exception e){
e.printStackTrace();
}
return null;
}

测试方法


//本地tomcat
// private String url = "http://127.0.0.1:8080/Car4s-1.0-SNAPSHOT/carApi/getCarAll";//获取全部数据
// private String url = "http://127.0.0.1:8080/Car4s-1.0-SNAPSHOT/carApi/getCar";//获取某一条记录
// private String url = "http://127.0.0.1:8080/Car4s-1.0-SNAPSHOT/carApi/postCarList";//推送数据 @Test
public void testPostList() throws IOException, HttpException { JSONArray jsonArray = new JSONArray();
//批量推送数据
for (int i = 1; i < 3; i++) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("carType", "测试00"+i);
//Integer modifyType 1增加 2更新
jsonObject.put("modifyType", new Integer(1));
jsonArray.add(jsonObject);
} //推送一条数据
// JSONObject jsonObject = new JSONObject();
// jsonObject.put("carSize", 4);
// jsonObject.put("carType", "测试3");
// jsonObject.put("keepRequirement", "无");
// jsonObject.put("keepTitle", "无");
// jsonObject.put("keepPrice", 80.0);
// jsonObject.put("repairePrice", 80.0);
// jsonObject.put("modifyType", new Integer(1));
// jsonArray.add(jsonObject); String string = JSONArray.toJSONString(jsonArray);
JSONObject post = post(url, string);
System.out.println(post);
}

服务器方法


/**
* 推送接口,支持一条或者多条数据
* @param request
* @param response
* @return
* @throws IOException
*/
@RequestMapping("/postCarList")
@ResponseBody
public JSONObject postCarList(HttpServletRequest request, HttpServletResponse response) throws IOException {
JSONObject jsonObject = new JSONObject();
request.setCharacterEncoding("UTF-8");
BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream(),"utf-8"));
String jsonStr = null;
StringBuilder result = new StringBuilder();
try {
while ((jsonStr = reader.readLine()) != null){
result.append(jsonStr);
}
}catch (Exception e){
e.printStackTrace();
}
reader.close(); JSONArray jsonArray = JSONArray.parseArray(result.toString());
try {
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject o = (JSONObject) jsonArray.get(i);
Car car = new Car();
car.setCarType(o.getString("carType"));
car.setLastTime(DateUtils.stringTodate(o.getString("lastTime"), null));
car.setCarSize(o.getInteger("carSize"));
car.setKeepRequirement(o.getString("carType"));
car.setKeepTitle(o.getString("keepTitle"));
car.setKeepPrice(o.getDouble("keepPrice"));
car.setRepairePrice(o.getDouble("repairePrice"));
if (o.getInteger("modifyType") == null){
jsonObject.put("msg", "Post失败,未指定修改类型");
jsonObject.put("code", -1);
return jsonObject;
}
int modifyType = o.getInteger("modifyType");
//增加
if (modifyType == 1){
carService.save(car);
}
//更新
else if (modifyType == 2){
car.setCarId(o.getInteger("carId"));
carService.update(car);
}else {
jsonObject.put("msg", "Post失败,修改类型指定类型错误");
jsonObject.put("code", -1);
return jsonObject;
}
}
jsonObject.put("msg", "Post成功");
jsonObject.put("code", 1);
}catch (Exception e){
e.printStackTrace();
}
return jsonObject;
}

httpclient post推送数据的更多相关文章

  1. java接口对接——调用别人接口推送数据

    实际开发中经常会遇到要和其他平台或系统对接的情况,实际操作就是互相调用别人的接口获取或者推送数据, 当我们调用别人接口推送数据时,需要对方给一个接口地址以及接口的规范文档,规范中要包括接口的明确入参及 ...

  2. SQL Server 2000向SQL Server 2008 R2推送数据

    [文章摘要]最近做的一个项目要获取存在于其他服务器的一些数据,为了安全起见,采用由其他“服务器”向我们服务器推送的方式实现.我们服务器使用的是SQL Server 2008 R2,其他“服务器”使用的 ...

  3. WebService推送数据,数据结构应该怎样定义?

    存放在Session有一些弊端,不能实时更新.server压力增大等... 要求:将从BO拿回来的数据存放在UI Cache里面,数据库更新了就通过RemoveCallback "告诉&qu ...

  4. Flume推送数据到SparkStreaming案例实战和内幕源码解密

    本期内容: 1. Flume on HDFS案例回顾 2. Flume推送数据到Spark Streaming实战 3. 原理绘图剖析 1. Flume on HDFS案例回顾 上节课要求大家自己安装 ...

  5. SuperSocket主动从服务器端推送数据到客户端

    关键字: 主动推送, 推送数据, 客户端推送, 获取Session, 发送数据, 回话快照 通过Session对象发送数据到客户端   前面已经说过,AppSession 代表了一个逻辑的 socke ...

  6. Asp.net Core3.1+Vue 使用SignalR推送数据

    本文就简单使用 往前端页面推送消息 SignalR 是什么 SignalR是一个.NET Core/.NET Framework的开源实时框架. SignalR的可使用Web Socket, Serv ...

  7. hive向es推送数据

    第一步:首先要保证网络是通的,很多公司里子网遍布,要和运维和工程侧同事确认好网络是通的,es的地址可以通过curl es地址的方式测试一下. 第二步:下载需要的jar包,必须的是es-hadoop的包 ...

  8. 使用SignalR ASP.NET Core来简单实现一个后台实时推送数据给Echarts展示图表的功能

    什么是 SignalR ASP.NET Core ASP.NET Core SignalR 是一种开放源代码库,可简化将实时 web 功能添加到应用程序的功能. 实时 web 功能使服务器端代码可以立 ...

  9. java推送数据到app--极光推送

    之前项目有用到需要把数据推送到app端 采用的是极光推送 特此把工具类和pom.xml需要的jar整理如下 pom.xml需要jar如下 <!-- 极光推送 --> <depende ...

随机推荐

  1. Shell编程—结构化命令

    1使用if-then语句 f-then语句有如下格式. if command then commands fi bash shell的if语句会运行if后面的那个命令.如果该命令的退出状态码是0(该命 ...

  2. 06.深入学习redis replication的完整流程和原理

    一.replication的完整流程 slave配置master ip和port # slaveof <masterip> <masterport> slaveof 127.0 ...

  3. 深入探究.Net Core Configuration读取配置的优先级

    前言     在之前的文章.Net Core Configuration源码探究一文中我们曾解读过Configuration的工作原理,也.Net Core Configuration Etcd数据源 ...

  4. Google Analytics谷歌分析事件之非互动事件

    非互动事件官方的解释如下 “非互动”一词是指可选的布尔值参数,此参数可以传递到用于发送事件命中的方法.通过此参数,您可以确定要如何为网站上包含事件衡量的网页定义跳出率.例如,假设您的首页上内嵌有一个视 ...

  5. 广州做假证c

    广州做假证[电/薇:187ヘ1184ヘ0909同号]办各类证件-办毕业证-办离婚证,办学位证书,办硕士毕业证,办理文凭学历,办资格证,办房产证不. 这是一个简单的取最大值程序,可以用于处理 i32 数 ...

  6. Python 中的数字到底是什么?

    花下猫语:在 Python 中,不同类型的数字可以直接做算术运算,并不需要作显式的类型转换.但是,它的"隐式类型转换"可能跟其它语言不同,因为 Python 中的数字是一种特殊的对 ...

  7. Oracle (实例名/服务名)SID和Service_Name的区别

    可以简单的这样理解:一个公司比喻成一台服务器,数据库是这个公司中的一个部门.1.SID:一个数据库可以有多个实例(如RAC),SID是用来标识这个数据库内部每个实例的名字,就好像一个部门里,每个人都有 ...

  8. 转载:Oracle常见字段类型

    转载节选自:https://bbs.csdn.net/topics/220059184 数据类型 参数 描述 char(n) n=1 to 2000字节 定长字符串,n字节长,如果不指定长度,缺省为1 ...

  9. ubuntu 18.04下修改python3指向

    起因 ubuntu18.04下默认带的是python3.6,但是因为需求需要升级为python3.7 步骤 安装 sudo apt install python3.7 修改环境变量 修改默认的pyth ...

  10. Echars 参数说明

    theme = { // 全图默认背景 // backgroundColor: 'rgba(0,0,0,0)', // 默认色板 color: ['#ff7f50','#87cefa','#da70d ...