Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JAVA发送Bonanza API POST请求_Java_Api_Http_Https_Http Post - Fatal编程技术网

使用JAVA发送Bonanza API POST请求

使用JAVA发送Bonanza API POST请求,java,api,http,https,http-post,Java,Api,Http,Https,Http Post,我正在尝试向Bonanza API发送一个简单的POST请求,请求AddFixedPriceItem。 他们给出了一个java示例,但它不起作用。 这是代码页 如果需要更多信息,请让我知道 感谢您以后的帮助状态代码500是一个内部服务器错误。500表示服务器的一部分(例如CGI程序)已崩溃,或者可能遇到配置错误 对于此错误,您可以读取HttpURLConnection。它将提供有关该问题的相关信息 为了更好地理解,您可以访问相关链接: 在使用getErrorStream()之后,我们收到了J

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

如果需要更多信息,请让我知道


感谢您以后的帮助

状态代码500是一个内部服务器错误。500表示服务器的一部分(例如CGI程序)已崩溃,或者可能遇到配置错误

对于此错误,您可以读取HttpURLConnection。它将提供有关该问题的相关信息

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

  • 在使用getErrorStream()之后,我们收到了JSON错误消息<代码>{“确认”:“失败”,“版本”:“1.0beta”,“时间戳”:“2016-03-13T03:03:51.000Z”,“错误消息”:{“消息”:“无法确定您发出的请求类型。通常这可能是由于数据在传递到API之前未转义造成的。如果您传递的数据带有引号或其他特殊字符,则应将其转换为JSON,然后转义,然后再通过API发送。”}这不会提供任何用于纠正错误的信息。
    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