我是 JSON 的新手。我正在调用公共休息 API https://api.gdc.cancer.gov/cases我想查询特定疾病类型的所有病例(例如下面提到的 TCGA-LAML)。在 SOAP Ui 中,当我以 JSON 格式发布以下请求时。它给了我完美的答案 { "filters": {"op":"in", "content":{ "field":"cases.project.project_id", "value ":["TCGA-LAML"] } } }但我必须通过 java 客户端调用 POST。即使在努力之后,我也无法正确设置输入参数。我在这里发布我的代码。你能帮我更正代码吗?package downloadtoolproject; import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.net.HttpURLConnection;import java.net.URL;public class Newtest { public static String sendPostRequest(String requestUrl, String payload) { StringBuffer jsonString = new StringBuffer(); try { URL url = new URL(requestUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Accept", "application/json"); connection.setRequestProperty("Content-Type", "application/json"); OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); writer.write(payload); writer.close(); BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = br.readLine()) != null) { jsonString.append(line); System.out.println(line); } br.close(); connection.disconnect(); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } return jsonString.toString() ; } public static void main(String [] args) { String payload = "{\"field\":\"project_id\",\"value\":[\"TCGA-LAML\"]}"; String requestUrl="https://api.gdc.cancer.gov/cases"; sendPostRequest(requestUrl, payload); }}
添加回答
举报
0/150
提交
取消