依赖和版本:httpclient、fastjson
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpclientartifactId>
<version>4.5.6version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.62version>
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));//获取服务器响应信息
测试输出结果如下
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}]
获取key:https://lbs.amap.com/api/javascript-api/guide/abc/prepare
搜索POI:https://lbs.amap.com/api/webservice/guide/api/search
生成一个key,复制出来:532d88ae29d61ee3a68c789ea84740df
打开postman,在postman里面获取接口链接
使用JavaAPI读取数据
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));//获取服务器响应信息