java—有人能确定我在这个api请求中犯了什么错误吗?

disho6za  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(345)

这个问题在这里已经有答案了

如何使用post-using-namevaluepair向httpurlconnection添加参数(16个答案)
上个月关门了。
我是restapi新手,尝试使用tradierapi中的getquotes方法来检索特定公司的市场数据。请求将回复400,代码如下。以下是api文档的链接:https://documentation.tradier.com/brokerage-api/markets/get-quotes

try {
        // MAKE REQUEST TO API
        URL url = new URL("https://sandbox.tradier.com/v1/markets/quotes");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("Authorization", "Bearer <token>");
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Accept", "application/json");
        connection.addRequestProperty("symbols", "AAPL");
        connection.connect();

        // GET INPUT STREAM RESPONSE FROM REQUEST
        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")));

        // TAKING BITS AND BYTES FROM BUFFERED READER TO BUILD IT INTO A STRING OBJECT
        StringBuilder json = new StringBuilder();

        String line;
        while ((line = br.readLine()) != null) {
            json.append(line);
        }

        JSONObject obj = new JSONObject(json.toString());
        System.out.println(obj);

    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
6yoyoihd

6yoyoihd1#

(第1种方法)
将您的url更改为:--
"https://api.tradier.com/v1/markets/quotes?symbols=aapl,vxx190517p00016000&greenes=false“(我的意思是手动添加)
(第二种方法)
或者,
添加如下所示的requestparamcode:----

try{

 URL url = new URL("https://sandbox.tradier.com/v1/markets/quotes");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("Authorization", "Bearer <token>");
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Accept", "application/json");
    Uri.Builder builder = new Uri.Builder()
            .appendQueryParameter("symbols", "AAPL,VXX190517P00016000")
            .appendQueryParameter("greeks", "false");
    String query = builder.build().getEncodedQuery();
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(
            new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();

connection.connect();
 // GET INPUT STREAM RESPONSE FROM REQUEST
        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")));

        // TAKING BITS AND BYTES FROM BUFFERED READER TO BUILD IT INTO A STRING OBJECT
        StringBuilder json = new StringBuilder();

        String line;
        while ((line = br.readLine()) != null) {
            json.append(line);
        }

        JSONObject obj = new JSONObject(json.toString());
        System.out.println(obj);

    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }

第三种方法

try{
 final HttpUriRequest request = RequestBuilder
        .get("https://api.tradier.com/v1/markets/quotes")
        .addHeader("Authorization", "Bearer <TOKEN>")
        .addHeader("Accept", "application/json")
        .addParameter("symbols", "AAPL,VXX190517P00016000")
        .addParameter("greeks", "false")
        .build();

    final HttpResponse response = HttpClientBuilder.create().build().execute(request);
    final String jsonString = EntityUtils.toString(response.getEntity());
    final JsonNode json = new ObjectMapper().readTree(jsonString);

    System.out.println(response.getStatusLine().getStatusCode());
    System.out.println(json);}
catch(Exception ex){
ex.printStack();
}

相关问题