2015-01-09 4 views
0

보난자 API에 간단한 GET 요청을 보내려고합니다.자바를 사용하여 보난자 API GET 요청 보내기

그들은 PHP 예제를 제공하지만 JAVA에서 작동하도록 만들 수 없습니다.

은 보난자 에서 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); 

는 코드 페이지 나는 "stavgallery"을 얻을 필요가 http://api.bonanza.com/docs/reference/get_booth

부스

입니다 이것은 내가 무엇이다. 아베가 지금까지 (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 

더 많은 정보가 필요한 경우

+0

업데이트? 오라클에 돈을 걸 겠어. – rightfold

+0

hehe 감사 수정을 위해 – zvikachu

답변

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); 
    } 
} 
+0

도와 줘서 고마워. 하지만 동일한 오류 코드 500을 내고 있습니다. – zvikachu

+0

아, 요청 방법을 POST로 변경해야 할 수도 있습니다 :'connection.setRequestMethod ("POST");' – edi

+0

나는 나를. – edi

관련 문제