zoukankan      html  css  js  c++  java
  • 解析Http请求调用高德地图的api(货车路径规划)

    package cn.nearf.ggz.utils;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.URI;
    import java.net.URL;
    import java.net.URLConnection;

    import com.alibaba.fastjson.JSONObject;


    public class MapStrategyUtils {

    public static String getHttpResponse(String serverUrl) {
      BufferedReader bf=null;
      StringBuffer result = null;
      try {
      //构造一个URI
        URI uri = new URI(serverUrl);
      //根据URI构造一个URL
        URL url = uri.toURL();
      //返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接
        URLConnection connection = url.openConnection();
      //设置请求属性
        connection.setRequestProperty("Content-type", "text/html");
        connection.setRequestProperty("Accept-Charset", "utf-8");
        connection.setRequestProperty("ContentType", "utf-8");
        connection.connect();
        result = new StringBuffer();
        //读取打开的连接的输入流并放入到缓冲区中
        bf= new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
        String line;
        while((line=bf.readLine())!=null) {
        result.append(line);
        }
        return result.toString();
          } catch (Exception e) {
            e.printStackTrace();
          }finally {
            try {
              if(bf!=null) {
              bf.close();
              }
            } catch (Exception e2) {
              e2.printStackTrace();
            }
          }
        return null;
    }

    public static String distance(Double width,Integer strategy,Integer size,Double weight,Integer axis,String origin,String destination,Double height,Double load,String key) {
      String url="http://restapi.amap.com/v4/direction/truck?"
      +"width="+width
      +"&strategy="+strategy
      +"&size="+size
      +"&weight="+weight
      +"&axis="+axis
      +"&origin="+origin
      +"&destination="+destination
      +"&height="+height
      +"&load="+load
      +"&key="+key
      ;
        String distanceString = getHttpResponse(url);
      //JSONObject json = JSONObject.parseObject(distanceString);
      return distanceString;
      }
    }

  • 相关阅读:
    luoguP1919 A*B Problem升级版 ntt
    luogu[愚人节题目3]现代妖怪殖民地 NTT
    FFT 快速傅里叶变换 学习笔记
    十二省联考 爆0记
    # NOI.AC省选赛 第五场T1 子集,与&最大值
    2019.6.16完成classstack任务
    USACO-集合
    USACO-修理牛棚
    dij模板
    洛谷P1948 [USACO08JAN]电话线Telephone Lines
  • 原文地址:https://www.cnblogs.com/CrisZjie180228/p/9151808.html
Copyright © 2011-2022 走看看