使用 JAVA 发送 Bonanza API GET 请求

Send an Bonanza API GET request using JAVA

我正在尝试向 Bonanza 发送一个简单的 GET 请求 API。

他们给出了一个 PHP 示例,但我似乎无法使其与 JAVA 一起使用。

这是代码页 http://api.bonanza.com/docs/reference/get_booth

我需要获得 "stavgallery" 个展位

这就是来自 Bonanza 的 PHP 示例 http://api.bonanza.com/docs/examples/php#get_booth

$dev_name = "xxx";
$api_url = "http://api.bonanza.com/api_requests/standard_request";
$headers = array("X-BONANZLE-API-DEV-NAME: " . $dev_name);
$args = array("userId" => "rooms_delivered");
$post_fields = "getBoothRequest=" .  json_encode($args, JSON_HEX_AMP);
echo "Request: $post_fields \n";
$connection = curl_init($api_url);
$curl_options = array(CURLOPT_HTTPHEADER=>$headers, CURLOPT_POSTFIELDS=>$post_fields,
                CURLOPT_POST=>1, CURLOPT_RETURNTRANSFER=>1);  # data will be returned as a string
curl_setopt_array($connection, $curl_options);
$json_response = curl_exec($connection);
if (curl_errno($connection) > 0) {
  echo curl_error($connection) . "\n";
  exit(2);
}
curl_close($connection);
$response = json_decode($json_response,true);
echo "Response: \n";
print_r($response);

这是我目前所拥有的(使用 Eclipse IDE):

        String devId = "HIDDEN";

    JSONArray stArray = new JSONArray ();
    JSONObject jsonObj = new JSONObject("{'userId':'stavgallery'}");
    stArray.put(jsonObj);

    JSONObject jsonObjFull = new JSONObject("{'getBoothRequest':"+stArray+"}");
    System.out.println(jsonObjFull.toString());
    int inputLine;
    URL url = new URL("http://api.bonanza.com/api_requests/standard_request");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.setDoOutput(true);
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setRequestProperty("Accept", "application/json");
    connection.setRequestProperty("X-BONANZLE-API-DEV-NAME", devId);
    OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
    writer.write(jsonObjFull.toString());
    writer.flush();
    writer.close();
    inputLine =  connection.getResponseCode();
    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    JSONObject sampleReturn = new JSONObject(in.readLine());
    System.out.println(sampleReturn);

获取错误

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 500 for URL: http://api.bonanza.com/api_requests/standard_request

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

感谢您以后的帮助

好像不需要把jsonObj放到数组里,直接用jsonObj构造请求即可:

编辑 已更新完整清单

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

public class Main {

    public static void main(String[] args) throws JSONException, IOException {
        // TODO: replace me
        String devId = "XXX";
        JSONObject query = new JSONObject("{'userId': 'stavgallery'}");
        JSONObject jsonObj = new JSONObject("{'getBoothRequest':" + query +"}");
        URL url = new URL("http://api.bonanza.com/api_requests/standard_request");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("X-BONANZLE-API-DEV-NAME", devId);
        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
        writer.write(jsonObj.toString());
        writer.flush();
        writer.close();
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        JSONObject sampleReturn = new JSONObject(in.readLine());
        System.out.println(sampleReturn);
    }
}