[英]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$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
該網址幾乎是正確的。
你在打電話
https://backpack.tf/api/classifieds/list/v1/
當記錄的API端點不帶有斜杠時。
https://backpack.tf/api/classifieds/list/v1
在瀏覽器中對其進行測試,如預期的那樣,我得到一個帶有斜杠的URL 404,並且在刪除斜杠時出現“請使用POST”錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.