高德地图调取行政区域应该注意的一个问题

首先,本人是刚开始接触高德地图的功能使用,在一次demo中遇到了问题:用 HttpClient 在查询区域中,并没有查询到想要的结果(区域信息),经过对输出信息的分析找到了原因。

demo 中输出的内容:

  public String city() {
        //获取http客户端
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet("https://restapi.amap.com/v3/config/district?key=<您的key>&keywords=山东&subdistrict=2&extensions=base&output=JSOON");
        //响应模型
        CloseableHttpResponse response = null;
        try {
            //由客户端执行(发送)Get请求
            response = httpClient.execute(httpGet);
            //从响应模型中获取响应实体
            HttpEntity entity = response.getEntity();
            System.out.println("响应状态为:" + response.getStatusLine());
            if (entity != null) {
                System.out.println("响应内容长度为:" + entity.getContentLength());
                System.out.println("响应内容为:" + entity.getContent());
                以字符串格式输出内容
                System.out.println(EntityUtils.toString(entity));            
}
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } finally {
            try {
            if (response != null) {
                response.close();
            }
            if (httpClient != null) {
                httpClient.close();
            }
            } catch (IOException e){
                e.printStackTrace();
            }
        }
    }

最开始我输出的结果的

响应状态为:HTTP/1.1 200 OK
响应内容长度为:-1
响应内容为: org.apache.http.client.entity.LazyDecompressingInputStream@1d625ffa
{"info":"USERKEY_PLAT_NOMATCH","infocode":"10009","status":"0","se等等

并没有报错,也没有想要的区域信息,但是得到这个输出消息的时候,可以明白哪里错了,主要看这句:

"info":"USERKEY_PLAT_NOMATCH"
这句很明显就是说key值不匹配不对

原因就在这里,我是用的我以前申请的web端的key,但是行政区域却需要web服务的key才行:


gaodekey.png

新建key的时候可以看到对应的应用:


gaodexinjiankey.png

其实就是key的应用范围问题,希望大家以后遇到key值不匹配的问题,能够少走弯路,谢谢。

你可能感兴趣的:(高德地图调取行政区域应该注意的一个问题)