使用JAVA发送Bonanza API GET请求

时间:2015-01-09 00:14:46

标签: java json api httprequest

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

他们提供了一个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

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

感谢您将来的帮助

1 个答案:

答案 0 :(得分:0)

好像你不需要将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);
    }
}