java
package org.example;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONArray;
import org.json.JSONObject;
public class Main {
public static void main(String[] args) {
try {
// 定义要发送的请求数据
JSONObject testCase = new JSONObject();
JSONObject slots = new JSONObject();
JSONArray poiWaypoint = new JSONArray();
slots.put("map_name", "高德地图");
slots.put("transportation", "驾车");
slots.put("poi_start", "上海市人民广场");
poiWaypoint.put("外滩");
poiWaypoint.put("陆家嘴");
slots.put("poi_waypoint", poiWaypoint);
slots.put("poi", "上海迪士尼乐园");
slots.put("start_time", JSONObject.NULL);
slots.put("arrival_time", JSONObject.NULL);
slots.put("route_mode", JSONObject.NULL);
slots.put("need_to_stop_at_destination", JSONObject.NULL);
testCase.put("requestId", JSONObject.NULL);
testCase.put("planning_tree", JSONObject.NULL);
testCase.put("task_description", "导航从上海市人民广场到上海迪士尼乐园,要经过外滩和陆家嘴。");
testCase.put("slots", slots);
String urlStr = "http://101.148.38.179:7860/planning_tree_search";
// 创建URL对象
URL url = new URL(urlStr);
// 打开连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置请求方法为 POST
conn.setRequestMethod("POST");
// 设置请求头
conn.setRequestProperty("Content-Type", "application/json; utf-8");
conn.setRequestProperty("Accept", "application/json");
conn.setDoOutput(true);
// 发送请求数据
try (OutputStream os = conn.getOutputStream()) {
byte[] input = testCase.toString().getBytes("utf-8");
os.write(input, 0, input.length);
}
// 获取响应代码
int responseCode = conn.getResponseCode();
System.out.println("Response Code: " + responseCode);
// 读取响应数据
StringBuilder response = new StringBuilder();
try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"))) {
String responseLine;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
}
// 输出响应数据
JSONObject responseJson = new JSONObject(response.toString());
System.out.println("Response Data: " + responseJson.toString(4));
System.out.println("你好,世界!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
本文作者:Dong
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC。本作品采用《知识共享署名-非商业性使用 4.0 国际许可协议》进行许可。您可以在非商业用途下自由转载和修改,但必须注明出处并提供原作者链接。 许可协议。转载请注明出处!