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;
}