JavaAPI获取接口数据

1、内网的第三方接口信息获取

依赖和版本:httpclient、fastjson

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.62</version>
</dependency>

代码示例:

CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
//get 和post,post加密的,可以传更大的消息,不过一般都是get
HttpGet httpGet = new HttpGet("http://localhost:8080/users/all");
CloseableHttpResponse response = closeableHttpClient.execute(httpGet);
HttpEntity msg = response.getEntity();
System.out.println(response.getStatusLine());//当前响应的状态
System.out.println(EntityUtils.toString(msg));//获取服务器响应信息

测试输出结果如下

image-20210506113311716

HTTP/1.1 200 
[{"userid":10,"username":"dd","birthday":"2000-04-05T16:00:00.000+00:00","userhigh":199.8},{"userid":11,"username":"aa","birthday":"2001-04-05T16:00:00.000+00:00","userhigh":194.8},{"userid":15,"username":"zll","birthday":"2021-04-29T00:14:58.000+00:00","userhigh":165.5},{"userid":16,"username":"fg","birthday":"2021-04-29T00:19:21.000+00:00","userhigh":165.5},{"userid":2,"username":"ls","birthday":"2000-01-01T16:00:00.000+00:00","userhigh":190.4},{"userid":1,"username":"zs","birthday":"1999-08-06T16:00:00.000+00:00","userhigh":180.5},{"userid":6,"username":"qw","birthday":"1993-08-06T16:00:00.000+00:00","userhigh":183.5},{"userid":3,"username":"ew","birthday":"1995-08-06T16:00:00.000+00:00","userhigh":170.5},{"userid":4,"username":"er","birthday":"1996-08-06T16:00:00.000+00:00","userhigh":185.5},{"userid":5,"username":"df","birthday":"1997-08-06T16:00:00.000+00:00","userhigh":160.5},{"userid":102,"username":"ccd","birthday":"2021-04-30T03:03:50.000+00:00","userhigh":188.2},{"userid":103,"username":"xxi","birthday":"2021-04-30T03:03:50.000+00:00","userhigh":171.2},{"userid":104,"username":"hehe","birthday":"2021-04-30T03:11:35.000+00:00","userhigh":188.1},{"userid":100,"username":"ccd","birthday":"2021-04-30T03:03:24.000+00:00","userhigh":188.2},{"userid":101,"username":"xxi","birthday":"2021-04-30T03:03:24.000+00:00","userhigh":171.2}]

2、外网开放API获取数据——高德地图测试

获取key:https://lbs.amap.com/api/javascript-api/guide/abc/prepare

搜索POI:https://lbs.amap.com/api/webservice/guide/api/search

image-20210506114615257

image-20210506120614134

image-20210506114917405

生成一个key,复制出来:532d88ae29d61ee3a68c789ea84740df

打开postman,在postman里面获取接口链接

image-20210506120706810

image-20210506120732993

使用JavaAPI读取数据

image-20210506120911867

CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
//get 和post,post加密的,可以传更大的消息,不过一般都是get
HttpGet httpGet = new HttpGet("https://restapi.amap.com/v3/place/text?key=532d88ae29d61ee3a68c789ea84740df&keywords=美食&city=nanjing&offset=20&page=1&extensions=all");
CloseableHttpResponse response = closeableHttpClient.execute(httpGet);
HttpEntity msg = response.getEntity();
System.out.println(response.getStatusLine());//当前响应的状态
System.out.println(EntityUtils.toString(msg));//获取服务器响应信息
ne());//当前响应的状态
System.out.println(EntityUtils.toString(msg));//获取服务器响应信息
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 通过get方式获取接口数据,可以使用JavaHTTP请求库来发送HTTP请求。HTTP请求分为GET和POST两种方式,GET请求的作用是向指定的实体资源发出请求并返回实体的响应结果,GET方法发送的数据会附加在URL之后,以'?'分割URL和传输数据,多个参数之间用'&'隔开。 Java中常用的HTTP请求库有Apache HttpClient、OkHttpHttpURLConnection等,其中HttpURLConnection是Java自带的HTTP请求类,使用比较方便。可以通过以下步骤来获取接口数据: 1. 创建URL对象,指定要访问的接口地址 2. 调用URL类的openConnection方法初始化一个HttpURLConnection对象,并设置请求类型为GET 3. 设置请求头部信息,如User-Agent、Accept-Encoding等 4. 发送请求并获取服务器响应的状态码 5. 读取服务器返回的数据并以字符串形式返回 例如,使用HttpURLConnection发起GET请求的示例代码如下: ``` URL url = new URL("http://example.com/api/data"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("User-Agent", "Mozilla/5.0"); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader in = new BufferedReader( new InputStreamReader(connection.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); System.out.println(response.toString()); } else { System.out.println("GET request unsuccessful"); } ``` 通过以上方法,Java可以方便地使用GET方式获取API接口数据。当然,在实际开发中,还需要根据具体需求对请求头部信息、响应码等进行自定义设置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值