zoukankan      html  css  js  c++  java
  • 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;
    }

  • 相关阅读:
    js、css引用文件的下载方式
    前端参考
    Mac配置
    chrome devtools调试Android Webview再也不FQ了!URL映射 + appspot devtools镜像解决chrome调试安卓前端HTML5页面时白屏的问题
    MyBatis 日志输出
    MyBatis 简单原理介绍
    MyBatis resultType 的使用
    sql必知必会笔记
    MyBatis基础:使用java提供的ThreadLocal类优化代码
    Mybatis基础(2)
  • 原文地址:https://www.cnblogs.com/tongcc/p/11708974.html
Copyright © 2011-2022 走看看