java后台工具类调用api接口,解析数据

httpclient后台调用接口,解析数据

一 、 引入jar包

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>
         <!-- JSON转换包 -->
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>

二、 httpclient请求接口工具类

2.1 get、post、“head”, “options”, “delete”, "trace"等方式
public class HttpClient {

    /**
     * 向目的URL发送post请求
     *
     * @param url          目的url
     * @param headerParams 请求头参数	key:value
     * @param bodyParams   请求体参数 	key:value
     * @return 
     */
    public static String sendPostRequest(String url, Map<String, String> headerParams,
                                         Map<String, String> bodyParams) {
        RestTemplate client = new RestTemplate();
        //新建Http头,add方法可以添加参数
        HttpHeaders headers = new HttpHeaders();
        //给请求头设置参数
        for (String key : headerParams.keySet()) {
            headers.add(key, headerParams.get(key));
        }
        //设置请求发送方式HttpMethod.GET、HttpMethod.DELETE等
        HttpMethod method = HttpMethod.POST;
        // 设置提交方式这里设置成application/json格式
        headers.setContentType(MediaType.APPLICATION_JSON);
        //将请求头部和参数合成一个请求
        HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(bodyParams, headers);
        //执行HTTP请求,将返回的结构使用String 类格式化(可设置为对应返回值格式的类)
        ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class);
        //返回类型也可以自动填充到实体类当中去,比如我自己创建了User类,当然字段名称要和返回字段一致
        //ResponseEntity<User> response = client.exchange(url, method, requestEntity, User.class);
        return response.getBody();
    }

2.2 PATCH等其他方式
/**
     * 向目的URL发送patch请求,只比其他方式多了一个允许aptch方式的方法。
     * 由于httpclient不支持patch请求,所以需要反射方式获取连接对象,增加patch方式
     * @param url          目的url
     * @param headerParams 请求头参数
     * @param bodyParams   请求体参数
     * @return AdToutiaoJsonTokenData
     */
    public static String sendPatchRequest(String url, Map<String, String> headerParams,
                                          Map<String, String> bodyParams) {

        //httpclient不支持patch请求,反射方式获取连接对象,增加patch方式
        allowMethods("PATCH");
        RestTemplate client = new RestTemplate();
        //新建Http头,add方法可以添加参数
        HttpHeaders headers = new HttpHeaders();
        //给请求头设置参数
        for (String key : headerParams.keySet()) {
            headers.add(key, headerParams.get(key));
        }
        //headers.add("X-HTTP-Method-Override", "PATCH");
        //设置请求发送方式
        HttpMethod method = HttpMethod.PATCH;
        // 设置提交方式这里设置成application/json格式
        headers.setContentType(MediaType.APPLICATION_JSON);
        //将请求头部和参数合成一个请求
        HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(bodyParams, headers);
        //执行HTTP请求,将返回的结构使用String 类格式化(可设置为对应返回值格式的类)
        ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class);

        return response.getBody();
    }
    //增加支持patch请求方式
    private static void allowMethods(String... methods) {
        try {
        	//获取连接类的属性,给属性添加aptch就允许aptch请求方式了	
            Field methodsField = HttpURLConnection.class.getDeclaredField("methods");

            Field modifiersField = Field.class.getDeclaredField("modifiers");
            modifiersField.setAccessible(true);
            modifiersField.setInt(methodsField, methodsField.getModifiers() & ~Modifier.FINAL);

            methodsField.setAccessible(true);

            String[] oldMethods = (String[]) methodsField.get(null);
            Set<String> methodsSet = new LinkedHashSet<>(Arrays.asList(oldMethods));
            methodsSet.addAll(Arrays.asList(methods));
            String[] newMethods = methodsSet.toArray(new String[0]);

            methodsField.set(null/*static field*/, newMethods);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new IllegalStateException(e);
        }
    }

2.3 解析数据

//工具类调用api接口,获取返回数据
 String result = HttpClient.sendPostRequest(createZoomMeetingUrl,header,body);
        JSONObject json = JSONObject.fromObject(result);
        //解析获取数据
        String startUrl = json.getString("start_url");
        String joinUrl = json.getString("join_url");
        //会议室id
        int id = json.getInt("id");
        //解析数据数据
        JSONArray jsonArray = json.getJSONArray("users");
        for(int i=0;i<jsonArray .size();i++){
         	String firstName = jsonArray.getJSONObject(i).getString("first_name");
         	...............
        }
       
  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值