zoukankan      html  css  js  c++  java
  • java调用外部接口,并得到list集合

    HttpClientUtil工具类
    package com.haiqi.jmles.util;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.client.utils.URIBuilder;
    import org.apache.http.entity.ContentType;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.protocol.HTTP;
    import org.apache.http.util.EntityUtils;
    import tk.mybatis.mapper.util.StringUtil;
    import java.io.IOException;
    import java.net.URI;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    
    
    public class HttpClientUtil {
    
        /**
         * 带参数get请求
         * @param url
         * @param param
         * @return
         */
        public static String doGet(String url, Map<String, String> param) {
            // 创建Httpclient对象
            CloseableHttpClient httpclient = HttpClients.createDefault();
    
            String resultString = "";
            CloseableHttpResponse response = null;
            try {
                // 创建uri
                URIBuilder builder = new URIBuilder(url);
                if (param != null) {
                    for (String key : param.keySet()) {
                        builder.addParameter(key, param.get(key));
                    }
                }
                URI uri = builder.build();
                // 创建http GET请求
                HttpGet httpGet = new HttpGet(uri);
    
                // 执行请求
                response = httpclient.execute(httpGet);
                // 判断返回状态是否为200
                if (response.getStatusLine().getStatusCode() == 200) {
                    resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (response != null) {
                        response.close();
                    }
                    httpclient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return resultString;
        }
    
        /**
         * 不带参数的get请求
         * @param url
         * @return String
         */
        public static String doGet(String url) {
            return doGet(url, null);
        }
        
        /**
         * 带参数的post请求
         * @param url
         * @param param
         * @return String
         */
        public static String doPost(String url, Map<String, String> param) {
            // 创建Httpclient对象
            CloseableHttpClient httpClient = HttpClients.createDefault();
            CloseableHttpResponse response = null;
            String resultString = "";
            try {
                // 创建Http Post请求
                HttpPost httpPost = new HttpPost(url);
    
                // 创建参数列表
                if (param != null) {
                    List<NameValuePair> paramList = new ArrayList<>();
                    for (String key : param.keySet()) {
                        paramList.add(new BasicNameValuePair(key, param.get(key)));
                    }
                    // 模拟表单 将参数设置为utf-8的形式
                    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, HTTP.UTF_8);
                    httpPost.setEntity(entity);
                }
                // 执行http请求
                response = httpClient.execute(httpPost);
    
                resultString = EntityUtils.toString(response.getEntity(), "utf-8");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return resultString;
        }
    
        /**
         * 不带参数的post请求
         * @param url
         * @return String
         */
        public static String doPost(String url) {
            return doPost(url, null);
        }
    }

    接口返回的json数据

    调用外部接口,返回list集合

        public List<WFTraffic> test3() {
            Map<String, String> parms = new HashMap<>();
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            DateFormat df= new SimpleDateFormat("yyyy-MM-dd");
            //(1)获取当前时间
            LocalDateTime date = LocalDateTime.now();
            String nowtime=dateTimeFormatter.format(date);
            System.out.println("系统当前时间      :"+nowtime);
            //(2)获取当前时间的前12小时时间
            LocalDateTime localDateTime = date.minusHours(12);
            String nowtime12=dateTimeFormatter.format(localDateTime);
            System.out.println("获取当前时间的前12小时时间      :"+nowtime12);
            parms.put("starttime",nowtime12);
            parms.put("endtime",nowtime);
            //外部接口地址
            String url="http://36.154.210.106:8090/zxpybm/sts/openShareData/getWfTraffic2?starttime="+nowtime12+"&endtime="+nowtime;
            //调用HttpClientUtil.doGet工具类
            String result = HttpClientUtil.doGet(url);
            JSONObject person = JSON.parseObject(result);
            JSONArray at = person.getJSONArray("result");
    
            List<WFTraffic> wFTrafficList = new ArrayList<>();
            for (int i = 0; i < at.size(); i++) {
                JSONObject iEach = at.getJSONObject(i);
                WFTraffic wFTraffic= new WFTraffic();
                //获取json里的参数,set到wFTraffic里面
                wFTraffic.setShipNameCN((String) iEach.get("SHIP_NAME_CN"));
                wFTraffic.setAdPort((String) iEach.get("AD_PORT"));
                wFTraffic.setShipNameCN((String) iEach.get("SHIP_NAME_EN"));
                wFTraffic.setAdTime((String) iEach.get("AD_TIME"));
                wFTraffic.setDeclare_time((String) iEach.get("DECLARE_TIME"));
                wFTraffic.setNextPort((String) iEach.get("NEXT_PORT"));
                wFTraffic.setWorkTimeBegin((String) iEach.get("WORK_TIME_BEGIN"));
                wFTraffic.setAdBerth((String) iEach.get("AD_BERTH"));
                wFTraffic.setGoodsClass((String) iEach.get("GOODS_CLASS"));
                wFTraffic.setTotalWeight((String) iEach.get("TOTAL_WEIGHT"));
                wFTraffic.setVoyageNo((String) iEach.get("VOYAGE_NO"));
                wFTraffic.setGoodsNameEN((String) iEach.get("GOODS_NAME_EN"));
                wFTraffic.setGoodsNameCN((String) iEach.get("GOODS_NAME_CN"));
                wFTraffic.setPortFlag((String) iEach.get("PORT_FLAG"));
                wFTraffic.setWorkTimeEnd((String) iEach.get("WORK_TIME_END"));
                wFTraffic.setPrenPort((String) iEach.get("PREV_PORT"));
                wFTraffic.setGoodsDirection((String) iEach.get("GOODS_DIRECTION"));
                wFTrafficList.add(wFTraffic);
            }
            return wFTrafficList;
        }
  • 相关阅读:
    学习网站
    Windows下python安装运行
    Python学习
    ES学习
    Eclipse安装lombok及常用注解
    Spark学习资料
    Spring Cloud学习资料
    使用Excel过滤重复数据
    Excel根据字符串截取单元格部分内容
    Spring中@Transactional(rollbackFor = Exception.class)的作用
  • 原文地址:https://www.cnblogs.com/huoyz/p/14243461.html
Copyright © 2011-2022 走看看