使用 POST Java 传递数组

Passing through an array with POST Java

我开门见山。 我正在尝试使用需要传递数组的 API,这样您就可以在单个 API 调用中传递多个对象。每当我尝试这样做时,我都会收到 400 个错误。

不是 100% 确定我做的是否正确,尝试了很多调查但找不到有同样问题的人。对不起所有的文字,但它应该让一切都更直接。任何帮助将不胜感激,谢谢。

首先,数组应该是这样的:

{
   "listings":[
      {
         "intent":0,
         "item": {
               "quality": 6,
               "item_name": "Name",
               "craf": 1,
               "priceindex":"currency"
                 },
         "currencies":{
            "m":1
                      },
         "details":"Test",
         "BO":1,
         "offers":1,
         "promoted":0
      }
   ]
}

这是我尝试制作数组的方法:

    JSONObject listing = new JSONObject();
    JSONObject itemObject = new JSONObject();
    JSONObject itemDetails = new JSONObject();
    JSONObject currency = new JSONObject();

    itemObject.put("intent", intent);

    itemDetails.put("quality", quality);
    itemDetails.put("item_name", item_name);
    itemDetails.put("craf", craftable);
    itemDetails.put("princeindex", princeindex);

    itemObject.put("item", itemDetails);

    currency.put("m", currencies);

    itemObject.put("details", "Test");
    itemObject.put("BO", buyout);
    itemObject.put("offers", offers);
    itemObject.put("promoted", promoted);
    itemObject.put("currencies", currency);

    listing.put("listings", itemObject);

    Set(listing);

POST 调用的实际代码在这里:

URL url = new URL("https://backpack.tf/api/classifieds/list/v1");
        Map<String, Object> params = new LinkedHashMap<>();
        params.put("token","#########");
        params.put("listings", this.listings);

        StringBuilder postData = new StringBuilder();
        for (Map.Entry<String, Object> param : params.entrySet()) {
            if (postData.length() != 0)
                postData.append('&');
            postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
            postData.append('=');
            postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
        }
        byte[] postDataBytes = postData.toString().getBytes("UTF-8");

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("User-Agent", "Mozilla/5.0");
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
        conn.setDoOutput(true);
        conn.getOutputStream().write(postDataBytes);

编辑(400错误):

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL: https://backpack.tf/api/classifieds/list/v1?token=5863418ee3387722bc77bcc0
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.run(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at Scraper.APIFinalBOT.set(APIFinalBOT.java:48)
    at Scraper.nodeJSBotPricer.Set(nodeJSBotPricer.java:97)
    at Scraper.nodeJSBotPricer.main(nodeJSBotPricer.java:82)
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://backpack.tf/api/classifieds/list/v1?token=5863418ee3387722bc77bcc0
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at java.net.HttpURLConnection.getResponseCode(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
    at Scraper.APIFinalBOT.set(APIFinalBOT.java:43)
    ... 2 more

URL几乎正确。

您正在打电话

 https://backpack.tf/api/classifieds/list/v1/

当记录的 API 端点没有尾部斜杠时。

 https://backpack.tf/api/classifieds/list/v1

在浏览器中测试这些,我得到一个带有斜杠的 URL 的 404 和删除斜杠时的 "please use POST" 错误,正如预期的那样。