import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; import java.io.File; /** * 阿里云物流轨迹地图API测试 * 快递单号:434815453222560(韵达快递) */ public class ExpressMapTest { public static void main(String[] args) { String host = "https://alicloudmarket8002.kdniao.com"; String path = "/api/track/8002"; String appcode = "e7b5746ef8854cf2a6da24342ab53af6"; String body = "{\"CustomInfo\":\"0000\",\"LogisticCode\":\"434815453222560\"}"; System.out.println("========================================"); System.out.println("阿里云物流轨迹地图API测试"); System.out.println("========================================"); System.out.println("请求地址: " + host + path); System.out.println("快递公司: 韵达快递"); System.out.println("快递单号: 434815453222560"); System.out.println("----------------------------------------"); try { URL url = new URL(host + path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Authorization", "APPCODE " + appcode); connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); connection.setDoOutput(true); try (OutputStream os = connection.getOutputStream()) { byte[] input = body.getBytes(StandardCharsets.UTF_8); os.write(input, 0, input.length); } int httpCode = connection.getResponseCode(); System.out.println("HTTP状态码: " + httpCode); System.out.println("----------------------------------------"); BufferedReader br; if (httpCode == 200) { br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)); } else { br = new BufferedReader(new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8)); } StringBuilder response = new StringBuilder(); String line; while ((line = br.readLine()) != null) { response.append(line); } br.close(); if (httpCode == 200) { System.out.println("✅ API调用成功!"); System.out.println("返回结果:"); System.out.println(response.toString()); java.io.FileWriter fileWriter = new java.io.FileWriter("map_result.json"); fileWriter.write(response.toString()); fileWriter.close(); System.out.println("\n✅ 结果已保存到: map_result.json"); // 获取驾车路线数据 String routeData = getDrivingRoute(); createMapHtml(response.toString(), routeData); } else { System.out.println("❌ API调用失败!"); System.out.println("错误信息: " + response.toString()); } System.out.println("========================================"); } catch (Exception e) { System.out.println("❌ 发生异常"); e.printStackTrace(); } } /** * 调用高德Web服务API获取驾车路线 */ private static String getDrivingRoute() { try { String key = "a0f9b8eecd097889d3aa21f4bd6665ff"; // Web服务API Key String origin = "113.264385,23.129163"; // 广州 String destination = "114.057868,22.543099"; // 深圳 String urlStr = "https://restapi.amap.com/v3/direction/driving?origin=" + origin + "&destination=" + destination + "&key=" + key + "&output=json"; System.out.println("\n🚗 调用高德驾车路线规划API..."); System.out.println("起点: 广州市"); System.out.println("终点: 深圳市"); URL url = new URL(urlStr); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); int httpCode = connection.getResponseCode(); if (httpCode == 200) { BufferedReader br = new BufferedReader( new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8) ); StringBuilder response = new StringBuilder(); String line; while ((line = br.readLine()) != null) { response.append(line); } br.close(); System.out.println("✅ 驾车路线获取成功"); return response.toString(); } else { System.out.println("❌ 驾车路线获取失败,状态码: " + httpCode); return null; } } catch (Exception e) { System.out.println("❌ 获取驾车路线异常: " + e.getMessage()); return null; } } /** * 创建HTML地图页面(使用Web服务API的路线数据) */ private static void createMapHtml(String jsonData, String routeData) { try { String routeJson = routeData != null ? routeData : "null"; String html = "\n" + "\n" + "\n" + " \n" + " \n" + " 物流轨迹地图 - 韵达快递\n" + " \n" + " \n" + " \n" + "\n" + "\n" + "
\n" + "
\n" + "

🗺️ 物流轨迹地图

\n" + "

实时追踪快递位置 - 真实驾车路线展示

\n" + " ✓ 在途中\n" + "
\n" + "
\n" + "
\n" + " 快递公司:\n" + " 韵达快递\n" + "
\n" + "
\n" + " 快递单号:\n" + " 434815453222560\n" + "
\n" + "
\n" + " 当前位置:\n" + " 加载中...\n" + "
\n" + "
🚗 使用Web服务API获取真实路线...
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
📦 物流轨迹
\n" + "
\n" + "
\n" + "
\n" + "
\n" + " \n" + "\n" + ""; java.io.FileWriter fileWriter = new java.io.FileWriter("map_view.html"); fileWriter.write(html); fileWriter.close(); String absPath = new File("map_view.html").getAbsolutePath(); System.out.println("\n✅ HTML地图页面已创建: map_view.html"); System.out.println("\n📌 请在浏览器中打开以下文件查看物流地图:"); System.out.println(" file://" + absPath); System.out.println("\n🔑 改进内容:"); System.out.println(" ✓ 使用Web服务API获取真实驾车路线"); System.out.println(" ✓ 在Java端调用路径规划API"); System.out.println(" ✓ 避免JS API的安全密钥问题"); System.out.println(" ✓ 显示预计距离和时间"); } catch (Exception e) { System.out.println("❌ 创建HTML失败: " + e.getMessage()); e.printStackTrace(); } } }