我是 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); }}
使用傳遞 json 對象的 java 客戶端調用 Rest API
慕的地6264312
2021-06-07 12:41:08
