2014-10-28 5 views
0

이 코드로 측량 용 monekey API에 연결하려고합니다. 작동하지 않습니다. API 콘솔에서 API를 가져 왔지만 "잘못된 API 키"라고 표시됩니다. 난 그냥 API 콘솔에서이 URL을 가지고Cant가 측량 원숭이 API에 연결

request being sent 
https://api.surveymonkey.net/v2/surveys/get_survey_list?api_key=---API-KEY---- 
The response code received is 200 
Output from Server .... 
{"status":3,"errmsg":"Expected object or value"} 

:

public void fetch() { 
     String url = "https://api.surveymonkey.net/v2/surveys/get_survey_list?api_key=" + apiKey; 
     System.out.println("request being sent"); 
     System.out.println(url); 
     JSONObject obj = new JSONObject(); 
     try { 
      // byte[] postDataBytes = obj.toJSONString().getBytes("UTF-8"); 
      URL ourl = new URL(url.toString()); 
      HttpURLConnection conn = (HttpURLConnection) ourl.openConnection(); 
      conn.setDoOutput(true); 
      conn.setRequestMethod("POST"); 

      conn.setRequestProperty("Authorization", "bearer " + accessToken); 
      conn.setRequestProperty("Content-Type", "application/json"); 

      conn.getRequestProperty(obj.toString().getBytes("UTF-8").toString()); 

      int k = conn.getResponseCode(); 
      System.out.println("The response code received is " + k); 
      if (conn.getResponseCode() != 200) { 
       throw new RuntimeException("Failed : HTTP error code : " 
         + conn.getResponseCode()); 
      } 
      BufferedReader br = new BufferedReader(new InputStreamReader(
        (conn.getInputStream()))); 

      String output; 

      System.out.println("Output from Server .... \n"); 

      output = br.readLine(); 
      System.out.println(output); 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

다음은 오류입니다.

답변

0

POST 데이터에 대해 빈 문자열이 전송되면 해당 오류가 생성됩니다. API는 최소한 빈 오브젝트 ("{}")를 필요로합니다. 위의 Miles가 지적한 문제가 오타 ('setRequestProperty'대신 'getRequestProperty'사용) 인 경우 빈 JSONObject의 toString이 ""또는 "{}"을 반환하는지 확인합니다.

1

http://developer.surveymonkey.com, 에 등록 된 개발자 계정과 연결된 API 키를 사용하고 있는지 확인하십시오. 콘솔에서 요청을 시도 할 때 사용하는 샘플 API 키가 아닙니다. 샘플 API 키는 API 콘솔에서만 앱과 함께 사용하도록되어 있지 않습니다.

+0

감사합니다. 하지만 여전히 오류가 발생합니다. 위에 질문이 업데이트되었습니다. – EternallyCurious

+0

문자열로 JSON 객체를 보내는 것처럼 보이지 않습니다. –

+0

이 부분에서 객체를 문자열로 보내고 있다고 생각합니다. conn.setRequestProperty ("Authorization", "bearer"+ accessToken); conn.setRequestProperty ("Content-Type", "application/json"); conn.getRequestProperty (obj.toString(). getBytes ("UTF-8"). toString()); 그 밖의 무엇을 기대합니까? 구체적으로 대답 해 주시겠습니까? – EternallyCurious