http接口的调用
1.按照文档先写入参数,这里主要介绍
Json格式的String字符串,包括拼接数组
String sqr_arry [] = new String[rowList.size()];
for(int i = 0; i < rowList.size(); i++) {
FieldList field_p = rowList.get(i);//查询每个家庭成员的姓名和身份证
String xm = field_p.get("pxm");
String sfzh = field_p.get("pzjhm");
String sq = "{\"xm\":\""+xm+"\",\"sfzh\":\""+sfzh+"\"}";
sqr_arry [i] = sq;//把各个家庭对象放进数组
}
String sqrs = "";
for(int i = 0;i < rowList.size(); i++ ){
sqrs += sqr_arry [i]+",";//从数组中取对象并做拼接
}
int idx = sqrs.lastIndexOf(",");//去掉最后一个逗号
sqrs = sqrs.substring(0,idx);
String sqr = "["+sqrs+"]";
String pararsa="{\"jkbm\":\"11\",\"batchid\":\""+ID+"\",\"sqrs\":"+sqr+",\"remark\":\""+cxyy+"\"}";//sqr为数组,解析完外层必须不带双引号“”;
String urlPost = "http://IP地址:端口号/***/***/***.action"; //接口地址
String resValue = HttpPost(urlPost, pararsa);//请求接口
/**
* 模拟HttpPost请求
* @param url
* @param jsonString
* @return
*/
public static String HttpPost(String url, String jsonString){
CloseableHttpResponse response = null;
CloseableHttpClient httpClient = HttpClientBuilder.create().build();//创建CloseableHttpClient
HttpPost httpPost = new HttpPost(url);//实现HttpPost
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(3000).setConnectTimeout(3000).build();
httpPost.setConfig(requestConfig); //设置httpPost的状态参数
httpPost.addHeader("Content-Type", "application/json");//设置httpPost的请求头中的MIME类型为json
StringEntity requestEntity = new StringEntity(jsonString, "utf-8");
httpPost.setEntity(requestEntity);//设置请求体
try {
response = httpClient.execute(httpPost, new BasicHttpContext());//执行请求返回结果
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
return null;
}
HttpEntity entity = response.getEntity();
if (entity != null) {
String resultStr = EntityUtils.toString(entity, "utf-8");
return resultStr;
} else {
return null;
}
} catch (Exception e) {
logger.error("httpPost method exception handle-- > " + e);
return null;
} finally {
if (response != null){
try {
response.close();//最后关闭response
} catch (IOException e) {
logger.error("httpPost method IOException handle -- > " + e);
}
}if(httpClient != null){try {httpClient.close();} catch (IOException e) {logger.error("httpPost method exception handle-- > " + e);}}}}
/**
* 模拟HttpGet 请求
* @param url
* @return
*/
public static String HttpGet(String url){
//单位毫秒
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(3000).setConnectTimeout(3000)
.setSocketTimeout(3000).build();//设置请求的状态参数
CloseableHttpClient httpclient = HttpClients.createDefault();//创建 CloseableHttpClient
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(requestConfig);
CloseableHttpResponse response = null;
try {
response = httpclient.execute(httpGet);//返回请求执行结果
int statusCode = response.getStatusLine().getStatusCode();//获取返回的状态值
if (statusCode != HttpStatus.SC_OK) {
return null;
} else {
String result = EntityUtils.toString(response.getEntity(), "UTF-8");
return result;
}
} catch (Exception e) {
logger.error("httpget Exception handle-- > " + e);
} finally {
if (response != null){
try {
response.close();//关闭response
} catch (IOException e) {
logger.error("httpget IOException handle-- > " + e);
}
}
if(httpclient != null){
try {
httpclient.close();//关闭httpclient
} catch (IOException e) {
logger.error("httpget IOException handle-- > " + e);
}
}
}
return null;
}
http接口的调用的更多相关文章
- 【转】java通用URL接口地址调用方式GET和POST方式
java通用URL接口地址调用方式GET和POST方式,包括建立请求和设置请求头部信息等等......... import java.io.ByteArrayOutputStream; import ...
- 如何让Java和C++接口互相调用:JNI使用指南
如何让Java和C++接口互相调用:JNI使用指南 转自:http://cn.cocos2d-x.org/article/index?type=cocos2d-x&url=/doc/cocos ...
- 如何记录selenium自动化测试过程中接口的调用信息
上一篇博客,我写了python自动化框架的一些知识和粗浅的看法,在上一篇中我也给自己提出一个需求:如果记录在测试过程中接口的调用情况?提出这个需求,我觉得是有意义的.你在测试过程中肯定会遇到一些莫名其 ...
- 在Winform混合式框架中整合外部API接口的调用
在我们常规的业务处理中,一般内部处理的接口多数都是以数据库相关的,基于混合式开发的Winform开发框架,虽然在客户端调用的时候,一般选择也是基于Web API的调用,不过后端我们可能不仅仅是针对我们 ...
- SAP接口的调用
最近做一个专案用到的SAO接口的调用,用到的上传参数获取回传的IRfcTable,以及以IRfcTable作为参数上传SAP,通过查阅很多资料,发现资料说明的也多是鱼龙混杂,许多没有实现就直接贴在上面 ...
- sso接口的调用
之前一直想sso接口已经写好了,登录注册功能是怎么调用的呢?原来在登录注册的jsp页面实现的接口的调用,页面的校验和验证功能在jsp页面即可实现. 注册页面: <%@ page language ...
- COM组件 IDispatch 及双接口的调用
转自:http://blog.csdn.net/cnhk1225/article/details/50555647 一.前言 前段时间,由于工作比较忙,没有能及时地写作.其间收到了很多网友的来信询问和 ...
- 【转载】COM 组件设计与应用(十一)—— IDispatch 及双接口的调用
原文:http://vckbase.com/index.php/wv/1236.html 一.前言 前段时间,由于工作比较忙,没有能及时地写作.其间收到了很多网友的来信询问和鼓励,在此一并表示感谢.咳 ...
- 微信接口出现“调用支付jsapi缺少参数appid”
微信接口出现“调用支付jsapi缺少参数appid” 注意:@Html.Raw(ViewBag.wxJsApiParam),//json串ViewBag.wxJsApiParam是一个在后台拼的一个j ...
- C与C++接口相互调用
转载于:http://blog.csdn.net/lincoln_2012/article/details/50801080 项目中经常使用C和C++混合编程,那么,在调用对方接口时,总是不可避免地出 ...
随机推荐
- viewpage+RadioButton+Fragment简单导航界面
https://blog.csdn.net/qibanxuehua/article/details/47333879
- Javaweb项目中修改表单时数据回显方法
1.前言 先来说下什么是数据回显,比如我要修改我的个人信息,点击修改按钮后进入修改界面,在这个界面中直接将原来的信息显示在表单中,而不是空表单,这就是数据回显 2.思路 当点击修改的时候,从数据库中查 ...
- go 函数传递结构体
我定义了一个结构体,想要在函数中改变结构体的值,记录一下,以防忘记 ep: type Matrix struct{ rowlen int columnlen int list []int } 这是一个 ...
- IntelliJ IDEA 更新
一. 下载最新版本的idea 1. 官网下载,官网地址:http://www.jetbrains.com/idea/download/#section=windows 2. 百度网盘直接下载:http ...
- java提取字符串数字,Java获取字符串中的数字
================================ ©Copyright 蕃薯耀 2020-01-17 https://www.cnblogs.com/fanshuyao/ 具体的方法如 ...
- CentOS7安装postgreSQL11
1.添加PostgreSQL Yum存储库 sudo yum install https://download.postgresql.org/pub/repos/yum/11/redhat/rhel- ...
- 服务器CentOS7上安装MySql
1.确保服务器系统处于最新状态 [root@localhost ~]# yum -y update如果显示以下内容说明已经更新完成Replaced:grub2.x86_64 1:2.02-0.64.e ...
- WPF DataGrid标题Header Binding失效
前言:因业务需要对WPF中的DataGrid控件中的行数进行统计,同时把统计的行数显示在列标题上. 如果我们用常规的Binding手段更新DataGridTextColunm的Header的话是不会生 ...
- mybatis-plus invalid bound statement (not found) insert解决办法
使用mybatis-plus时,使用IService.insert方法时,提示找不到insert方法,原因是,mybatis-plus提供了两个BaseMapper和IService. 改成引用imp ...
- LeetCode:27 移除元素
给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成 ...