import java.net.URLEncoder; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class AMapCityQuery { private static final String API_KEY = "YOUR_API_KEY"; public static void main(String[] args) { String queryUrlString = buildQueryUrlString(); try { URL queryUrl = new URL(queryUrlString); HttpURLConnection connection = (HttpURLConnection) queryUrl.openConnection(); connection.setRequestMethod("GET"); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); System.out.println(response.toString()); } else { System.out.println("Error: " + responseCode); } } catch (Exception e) { e.printStackTrace(); } } private static String buildQueryUrlString() { StringBuilder urlStringBuilder = new StringBuilder(); urlStringBuilder.append("https://restapi.amap.com/v3/config/district?"); urlStringBuilder.append("key=").append(API_KEY); urlStringBuilder.append("&keywords=").append(URLEncoder.encode("中国", "UTF-8")); urlStringBuilder.append("&subdistrict=").append("1"); urlStringBuilder.append("&extensions=").append("base"); return urlStringBuilder.toString(); } }
上面是“Java代码如何利用高德地图API实现行政区划查询的热门城市推荐”的全面内容,想了解更多关于 后端开发 内容,请继续关注web建站教程。
当前网址:https://m.ipkd.cn/webs_12243.html
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!