高德猎鹰轨迹查询相关接口 httpclient Get

高德猎鹰轨迹官网:服务管理-API文档-开发指南-猎鹰轨迹服务 | 高德地图API

轨迹查询 httpclient的post 

// post方法请求  创建轨迹
private static void createTrace() {
        String key = "高德注册的key";
        String sid = "服务id"; // 服务id
        String tid = "轨迹id"; // 轨迹id
        String tName = "第一个轨迹"; // 轨迹id

        //创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //创建HttpPost对象
        HttpPost httpPost = new HttpPost(LieYingConstant.trace.ADD + 
                "?key=" + key + "&sid=" + sid + "&tid=" + tid + "&tname=" + tName);

        //设置请求头信息
        httpPost.setHeader("Content-Type", "application/json");

        //执行请求
        try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
            //获取响应结果
            HttpEntity responseEntity = response.getEntity();
            String responseString = EntityUtils.toString(responseEntity, "UTF-8");
            System.out.println(responseString);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

轨迹查询 httpclient的get,猎鹰轨迹纠偏查询有一个比较坑的地方,就是纠偏时,他默认只查询第一页的,如果要查询整个轨迹,后端需要拿到返回的点的数量,计算总的页数,再循环查询所有的点位,

 private static void tranceSearch(LieYingDTO lieYingDTO) {
        //创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //创建HttpPost对象
//        HttpGet httpGet = new HttpGet(LieYingConstant.trace.LIST + "?key=" + key + "&sid=" + sid +
//                "&tid=" + tid + "&trid=" + trid + "&correction=denoise=1,mapmatch=1,attribute=1,threshold=100,mode=driving&recoup=1");
        String requestUrl = LieYingConstant.trace.LIST + "?key=" + lieYingDTO.getKey() + "&sid=" + lieYingDTO.getSid() +
                "&tid=" + lieYingDTO.getTid() + "&trid=" + lieYingDTO.getTrid()
                + "&pagesize=999" ;
        HttpGet httpGet = new HttpGet(requestUrl);
        System.out.println("url---->" + requestUrl);
        //设置请求头信息
        httpGet.setHeader("Content-Type", "application/json");

        //执行请求
        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            //获取响应结果
            HttpEntity responseEntity = response.getEntity();
            String responseString = EntityUtils.toString(responseEntity, "UTF-8");
            JSONObject parse = (JSONObject)JSONObject.parse(responseString);
            JSONObject data = (JSONObject)parse.get("data");
            JSONArray tracks = (JSONArray) data.get("tracks");
            JSONObject obj = (JSONObject)tracks.get(0);
            JSONArray jsonArray = (JSONArray)obj.get("points");
            List<String[]> list = new ArrayList();
            for (Object o : jsonArray) {
                JSONObject object = (JSONObject) o;
                String location = object.get("location").toString();
                String[] split = location.split(",");
                list.add(split);
            }
            System.out.println(responseString);
            System.out.println(JSON.toJSON(list));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

post方法另外一个示例

public static String postJson(String method, String jsonStr) throws Exception {
        CloseableHttpClient sslClient = sslClient();
        CloseableHttpResponse response = null;
        try {
            HttpPost httpPost = new HttpPost(baseUrl + method);
            httpPost.setHeader("Content-Type", "application/json");
            httpPost.setHeader("appid", appid);
            httpPost.setHeader("sign", getHmacSign(jsonStr, secret));

            StringEntity se = new StringEntity(jsonStr, "UTF-8");
            httpPost.setEntity(se);

            response = sslClient.execute(httpPost);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    return EntityUtils.toString(resEntity, charSet);
                }
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        } finally {
            if (sslClient != null) {
                sslClient.close();
            }
            if (response != null) {
                response.close();
            }
        }
        return null;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值