【问题标题】:Send an Bonanza API POST request using JAVA使用 JAVA 发送 Bonanza API POST 请求
【发布时间】:2016-03-11 18:01:18
【问题描述】:

我正在尝试为 AddFixedPriceItem 向 Bonanza API 发送一个简单的 POST 请求。 他们给出了一个java示例,但它不起作用。 这是代码页http://api.bonanza.com/docs/examples/java

import org.json.JSONObject;
import org.json.JSONStringer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.HttpURLConnection;

class AddFixedPriceItem {
    public static void main(String[] args) {
        try {
            String devId = "t************I";
            String certId = "l***********F";

            URL url = new URL("https://api.bonanza.com/api_requests/secure_request");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setRequestProperty("Accept", "application/json");
            connection.setRequestProperty("X-BONANZLE-API-DEV-NAME", devId);
            connection.setRequestProperty("X-BONANZLE-API-CERT-NAME", certId);

            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());

            String jsonPayload = new JSONStringer()
                .object()
                    .key("requesterCredentials")
                    .object()
                        .key("bonanzleAuthToken")
                        .value("myAuthToken")
                    .endObject()

                    .key("item")
                    .object()
                        .key("title")
                        .value("Lightsaber")

                        .key("primaryCategory")
                        .object()
                            .key("categoryId")
                            .value("163128")
                        .endObject()

                        .key("description")
                        .value("An elegant weapon, for a more civilized age<br>* SELLER <strong>NOT LIABLE</strong> FOR DISMEMBERMENT *")
                        .key("price")
                        .value("42")
                        .key("quantity")
                        .value("3")
                        .key("shippingType")
                        .value("Free")

                        .key("itemSpecifics")
                        .array()
                            .array()
                                .value("condition")
                                .value("used")
                            .endArray()
                            .array()
                                .value("danger")
                                .value("extreme")
                            .endArray()
                        .endArray()

                        .key("pictureDetails")
                        .object()
                            .key("pictureURL")
                            .array()
                                .value("http://images.discountstarwarscostumes.com/products/9284/1-1/luke-skywalker-blue-lightsaber.jpg")
                                .value("http://www.rankopedia.com/CandidatePix/29862.gif")
                            .endArray()
                        .endObject()

                        .key("variations")
                            .array()
                                .object()
                                    .key("quantity")
                                    .value("2")
                                    .key("nameValueList")
                                    .array()
                                        .object()
                                            .key("name")
                                            .value("Colour")
                                            .key("value")
                                            .value("Blue")
                                        .endObject()
                                        .object()
                                            .key("name")
                                            .value("Style")
                                            .key("value")
                                            .value("Single")
                                        .endObject()
                                    .endArray()
                                .endObject()
                                .object()
                                    .key("quantity")
                                    .value("1")
                                    .key("nameValueList")
                                    .array()
                                        .object()
                                            .key("name")
                                            .value("Colour")
                                            .key("value")
                                            .value("Red")
                                        .endObject()
                                        .object()
                                            .key("name")
                                            .value("Style")
                                            .key("value")
                                            .value("Double")
                                        .endObject()
                                    .endArray()
                                .endObject()
                            .endArray()
                    .endObject()
                .endObject()
                .toString();

            String requestName = "addFixedPriceItemRequest";

            String toWrite = requestName + "=" + jsonPayload;

            writer.write(toWrite);
            writer.flush();
            writer.close();

            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String response = in.readLine();

            JSONObject jsonResponse = new JSONObject(response);

            if (jsonResponse.optString("ack").equals("Success") 
                    && jsonResponse.optJSONObject("addFixedPriceItemResponse") != null) {

                // Success! Now read more keys from the json object
                JSONObject outputFields = jsonResponse.optJSONObject("addFixedPriceItemResponse");
                System.out.println("Item ID: " + outputFields.optInt("itemId"));
                System.out.println("Category ID: " + outputFields.optInt("categoryId"));
                System.out.println("Selling State: " + outputFields.optString("sellingState"));
            } else {
                System.out.println(jsonResponse);
            }

        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

遇到错误

java.io.IOException: Server returned HTTP response code: 500 for URL: https://api.bonanza.com/api_requests/secure_request

如果需要更多信息,请告诉我

感谢您以后的帮助

【问题讨论】:

    标签: java api http https http-post


    【解决方案1】:

    状态码 500 是内部服务器错误。 500表示服务器的一部分(例如CGI程序)有crashedmay be encountered a configuration error

    对于这个错误你可以阅读 HttpURLConnection 的getErrorStream()。它将提供有关该问题的相关信息。

    为了更好地理解,您可以点击相关链接:

    1. Using java.net.URLConnection to fire and handle HTTP requests
    2. java.io.IOException: Server returned HTTP response code: 500

    【讨论】:

    • 使用 getErrorStream() 后,我们收到 JSON 错误消息。 {"ack":"Failure","version":"1.0beta","timestamp":"2016-03-13T03:03:51.000Z","errorMessage":{"message":"Cannot determine what type of request you are making. Often this can be the result of data that has not been escaped before being passed to the API. If you are passing data with quotation marks or other special characters, you should translate it to JSON, then escape it, before sending it over the API."}} 这并没有提供任何用于纠正错误的信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-29
    • 2017-10-16
    • 2019-10-19
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多