Java调用第三方接口示范(聚合数据天气预报接口) 您所在的位置:网站首页 面试题api如何调用第三方接口 Java调用第三方接口示范(聚合数据天气预报接口)

Java调用第三方接口示范(聚合数据天气预报接口)

2024-04-20 05:06| 来源: 网络整理| 查看: 265

在项目开发中经常会遇到调用第三方接口的情况,比如说调用第三方的天气预报接口。

本文以聚合数据接口为例,详细讲解下,接口地址:http://www.juhe.cn

使用流程

【1】准备工作:在项目的工具包下导入HttpClientUtil这个工具类,或者也可以使用Spring框架的restTemplate来调用,上面有调用接口的方法【分为Get和Post方式的有参和无参调用】:

package com.njsc.credit.util; import java.io.IOException; import java.net.URI; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.clienthods.CloseableHttpResponse; import org.apache.http.clienthods.HttpGet; import org.apache.http.clienthods.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.util.EntityUtils; public class HttpClientUtil { /** * 带参数的get请求 * @param url * @param param * @return String */ public static String doGet(String url, Map 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 param) { // 创建Httpclient对象 CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null; String resultString = ""; try { // 创建Http Post请求 HttpPost httpPost = new HttpPost(url); // 创建参数列表 if (param != null) { List paramList = new ArrayList(); for (String key : param.keySet()) { paramList.add(new BasicNameValuePair(key, param.get(key))); } // 模拟表单 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList); 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类型的post请求 * @param url * @param json * @return String */ public static String doPostJson(String url, String json) { // 创建Httpclient对象 CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null; String resultString = ""; try { // 创建Http Post请求 HttpPost httpPost = new HttpPost(url); // 创建请求内容 StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON); 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; } }

【2】创建url和访问key 以及参数等:

image.png

代码如下:

/** * 聚合接口校验身份证 * @param idCard * @param realName * @return boolean */ public boolean identityCheck(String idCard, String realName){ logger.info("-----------------调用聚合数据 身份证验证API BEGIN--------------->"); String key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; String url = "http://op.juhe.cn/idcard/query" + "?key=" + key + "&idcard=" + idCard + "&realname=" + realName; logger.info("请求url:" + url); boolean match = false; //是否匹配 try { String result = HttpClientUtil.doGet(url); System.out.println("请求结果:" + result); IdentityCheckResult identityCheckResult = JsonUtils.parse(result, IdentityCheckResult.class); IdentityCheck identityCheck = JsonUtils.parse(result, "result", IdentityCheck.class); logger.info(identityCheckResult); logger.info(identityCheck.toString()); if(identityCheckResult.correct() && identityCheck.getRes() == 1){ match = true; } } catch (Exception e) { e.printStackTrace(); } logger.info("


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有