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

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

一 、 引入jar包

        org.apache.httpcomponentshttpclient4.5.3net.sf.json-libjson-lib2.4jdk15

二、 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 headerParams,Map 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> requestEntity = new HttpEntity<>(bodyParams, headers);//执行HTTP请求,将返回的结构使用String 类格式化(可设置为对应返回值格式的类)ResponseEntity response = client.exchange(url, method, requestEntity, String.class);//返回类型也可以自动填充到实体类当中去,比如我自己创建了User类,当然字段名称要和返回字段一致//ResponseEntity 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 headerParams,Map 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> requestEntity = new HttpEntity<>(bodyParams, headers);//执行HTTP请求,将返回的结构使用String 类格式化(可设置为对应返回值格式的类)ResponseEntity 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 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");//会议室idint id = json.getInt("id");//解析数据数据JSONArray jsonArray = json.getJSONArray("users");for(int i=0;i


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部