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));//获取服务器响应信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值