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;
      }
    }

  • 相关阅读:
    mysql 的远程链接字符
    SqlSessionFactoryUtil
    mysql 的链接字符
    web 项目运用通用的xml配置
    配置文件转意符使用
    new和newInstance的区别
    子选择器与后代选择器的区别
    Vivado_HLS 学习笔记1-数据类型
    Vivado_HLS 学习笔记3-循环的pipeline与展开
    Vivado_HLS 学习笔记2-接口综合
  • 原文地址:https://www.cnblogs.com/CrisZjie180228/p/9151808.html
Copyright © 2011-2022 走看看