使用POST Java传递数组

时间:2017-08-22 00:02:47

标签: java api httpurlconnection

我会直言不讳。 我正在尝试使用需要传递数组的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$10.run(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection$10.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

1 个答案:

答案 0 :(得分:0)

网址几乎正确。

您正在致电

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

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

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

在浏览器中对这些进行测试时,如果删除了斜杠,我会得到带有斜杠的URL 404和“请使用POST”错误。