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

posted @ 2018-06-07 17:22  随风残虹  阅读(659)  评论(0编辑  收藏  举报