拷贝代码直接运行,可以查询上海公交站信息,每页返回20条,要分页查询需要把页码改一下。
高德API地址:搜索POI-高级 API 文档-开发指南-Web服务 API | 高德地图API
import com.alibaba.fastjson.JSONObject;
import com.zyq.entity.BusStopResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class GaoDeApiExample {
private static final String API_KEY = "你的高德API Key"; // 替换为你的高德API Key
private static final String CITY = "上海";
private static final int offset = 20;//默认返回20条
private static final int types = 150700;//公交-高德POI分类表
private static final String BUS_STATION_URL = "https://restapi.amap.com/v5/place/text?";
public static void main(String[] args) {
int page = 10;
String requestUrl = BUS_STATION_URL +"key=" + API_KEY+"&types="+ types +"&city=" + CITY +"&offset=" + offset+"&page=" + page;
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpGet httpGet = new HttpGet(requestUrl);
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
if (response.getStatusLine().getStatusCode() == 200) {
// 解析响应结果
String result = EntityUtils.toString(response.getEntity(), "UTF-8");
BusStopResponse busStopResponse= JSONObject.parseObject(result, BusStopResponse.class);
System.out.println(busStopResponse);
} else {
System.out.println("请求失败: " + response.getStatusLine().getStatusCode());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
实体类:
package com.zyq.entity;
import java.util.List;
public class BusStopResponse {
private String count;
private String infocode;
private String status;
private String info;
private List<Poi> pois;
// Getter and Setter for count
public String getCount() {
return count;
}
public void setCount(String count) {
this.count = count;
}
// Getter and Setter for infocode
public String getInfocode() {
return infocode;
}
public void setInfocode(String infocode) {
this.infocode = infocode;
}
// Getter and Setter for status
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
// Getter and Setter for info
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
// Getter and Setter for pois
public List<Poi> getPois() {
return pois;
}
public void setPois(List<Poi> pois) {
this.pois = pois;
}
}
package com.zyq.entity;
public class Poi {
private String parent;
private String address;
private String distance;
private String pcode;
private String adcode;
private String pname;
private String cityname;
private String type;
private String typecode;
private String adname;
private String citycode;
private String name;
private String location;
private String id;
// Getters and Setters
public String getParent() {
return parent;
}
public void setParent(String parent) {
this.parent = parent;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getDistance() {
return distance;
}
public void setDistance(String distance) {
this.distance = distance;
}
public String getPcode() {
return pcode;
}
public void setPcode(String pcode) {
this.pcode = pcode;
}
public String getAdcode() {
return adcode;
}
public void setAdcode(String adcode) {
this.adcode = adcode;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public String getCityname() {
return cityname;
}
public void setCityname(String cityname) {
this.cityname = cityname;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getTypecode() {
return typecode;
}
public void setTypecode(String typecode) {
this.typecode = typecode;
}
public String getAdname() {
return adname;
}
public void setAdname(String adname) {
this.adname = adname;
}
public String getCitycode() {
return citycode;
}
public void setCitycode(String citycode) {
this.citycode = citycode;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}