为了账号安全,请及时绑定邮箱和手机立即绑定

使用传递 json 对象的 java 客户端调用 Rest API

使用传递 json 对象的 java 客户端调用 Rest API

慕的地6264312 2021-06-07 12:41:08
我是 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);    }}
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 183 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信