解析Http请求调用高德地图的api(货车路径规划)

本文介绍了一个名为MapStrategyUtils的Java工具类,该类通过HTTP请求获取地图服务的数据,并能够根据不同的参数计算路径距离。主要功能包括构造HTTP请求、处理响应结果等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

转载于:https://www.cnblogs.com/CrisZjie180228/p/9151808.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值