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接口的调用的更多相关文章

  1. 【转】java通用URL接口地址调用方式GET和POST方式

    java通用URL接口地址调用方式GET和POST方式,包括建立请求和设置请求头部信息等等......... import java.io.ByteArrayOutputStream; import ...

  2. 如何让Java和C++接口互相调用:JNI使用指南

    如何让Java和C++接口互相调用:JNI使用指南 转自:http://cn.cocos2d-x.org/article/index?type=cocos2d-x&url=/doc/cocos ...

  3. 如何记录selenium自动化测试过程中接口的调用信息

    上一篇博客,我写了python自动化框架的一些知识和粗浅的看法,在上一篇中我也给自己提出一个需求:如果记录在测试过程中接口的调用情况?提出这个需求,我觉得是有意义的.你在测试过程中肯定会遇到一些莫名其 ...

  4. 在Winform混合式框架中整合外部API接口的调用

    在我们常规的业务处理中,一般内部处理的接口多数都是以数据库相关的,基于混合式开发的Winform开发框架,虽然在客户端调用的时候,一般选择也是基于Web API的调用,不过后端我们可能不仅仅是针对我们 ...

  5. SAP接口的调用

    最近做一个专案用到的SAO接口的调用,用到的上传参数获取回传的IRfcTable,以及以IRfcTable作为参数上传SAP,通过查阅很多资料,发现资料说明的也多是鱼龙混杂,许多没有实现就直接贴在上面 ...

  6. sso接口的调用

    之前一直想sso接口已经写好了,登录注册功能是怎么调用的呢?原来在登录注册的jsp页面实现的接口的调用,页面的校验和验证功能在jsp页面即可实现. 注册页面: <%@ page language ...

  7. COM组件 IDispatch 及双接口的调用

    转自:http://blog.csdn.net/cnhk1225/article/details/50555647 一.前言 前段时间,由于工作比较忙,没有能及时地写作.其间收到了很多网友的来信询问和 ...

  8. 【转载】COM 组件设计与应用(十一)—— IDispatch 及双接口的调用

    原文:http://vckbase.com/index.php/wv/1236.html 一.前言 前段时间,由于工作比较忙,没有能及时地写作.其间收到了很多网友的来信询问和鼓励,在此一并表示感谢.咳 ...

  9. 微信接口出现“调用支付jsapi缺少参数appid”

    微信接口出现“调用支付jsapi缺少参数appid” 注意:@Html.Raw(ViewBag.wxJsApiParam),//json串ViewBag.wxJsApiParam是一个在后台拼的一个j ...

  10. C与C++接口相互调用

    转载于:http://blog.csdn.net/lincoln_2012/article/details/50801080 项目中经常使用C和C++混合编程,那么,在调用对方接口时,总是不可避免地出 ...

随机推荐

  1. Lucene之分析器

    什么是分析器? 分析(Analysis)在Lucene中指的是将域(Field)文本转换为最基本的索引表示单元—项(Term)的过程. 分析器(Analyzer)对分析操作进行了封装,通过执行一系列操 ...

  2. Tomcat 项目部署、账户配置、状态监测

    tomcat部署项目 方式一.自动部署(最常用) 直接把war包或部署的文件夹放到webapps下. tomcat启动后会自动监听webapps下的文件|目录,放入打包好的项目会自动部署,移除打包好的 ...

  3. docker入门整理(1)--安装

    1.安装批量命令: CentOS7操作系统下. 包含卸载旧版本.安装依赖包.添加最新Yum源.安装docker-ce最新稳定版本.启动docker等: sudo yum remove docker \ ...

  4. Android中创建一个BroadcastReceiver

    首先创建一个java类继承BroadcastReceiver类 package com.example.service; import android.content.BroadcastReceive ...

  5. MySQL优化、锁

    1.  MySQL优化-查看执行记录 MySQL 提供了一个 EXPLAIN 命令, 它可以对 SELECT 语句进行分析, 并输出 SELECT 执行的详细信息, 以供开发人员针对性优化. 使用ex ...

  6. 标准化建筑的 FRESH 原则

    前记 大家好,我是小镭. 我在钢结构领域从业十余年,虽然工作内容是关于建筑的,但如果问我什么是建筑,我却一时答不出来. 记得小时候我读过一篇文章,说建筑是凝固的音乐. 后来我看了些书,觉得建筑是空间. ...

  7. 纪中23日c组T3 2161. 【2017.7.11普及】围攻 斐波那契数列

    2161. 围攻 (File IO): input:siege.in output:siege.out 时间限制: 1000 ms  空间限制: 262144 KB  具体限制   Goto Prob ...

  8. 【UWP】在 UWP 中使用 Exceptionless 进行遥测

    2020年1月17日更新: nightly build 版本已发布 https://www.myget.org/feed/exceptionless/package/nuget/Exceptionle ...

  9. t-SNE and PCA

    1.t-SNE 知乎 t-分布领域嵌入算法 虽然主打非线性高维数据降维,但是很少用,因为 比较适合应用于可视化,测试模型的效果 保证在低维上数据的分布与原始特征空间分布的相似性高 因此用来查看分类器的 ...

  10. idea插件不兼容问题

    https://plugins.jetbrains.com/ 找对应版本的插件