0

trying the request by passing the surveyID as a parameter from java using following input string. Getting following error

{"status": 3, "errmsg": "No JSON object could be decoded: line 1 column 0 (char 0)"}

String input ="{\"survey_id\": p_sSurveyID, \"fields\":[\"url\"]}"; -- not working

where as same is working fine if the surveyID is hard coded

String input ="{\"survey_id\":\"12345678\", \"fields\":[\"url\"]}"; -- working

gunr2171
  • 16,104
  • 25
  • 61
  • 88

1 Answers1

0

Probably you are not concatenating the psSurveyID properly.

String input ="{\"survey_id\": p_sSurveyID, \"fields\":[\"url\"]}"; -- not working

should be

String input ="{\"survey_id\":"+ p_sSurveyID+",\"fields\":[\"url\"]}"; -- should work


  System.out.println("p_sSurveyID --- " + p_sSurveyID); 
  try 
  { 
      List<NameValuePair> parameters = new ArrayList<NameValuePair>();
      parameters.add(new NameValuePair("api_key",p_sApiKey));
      URL url = new URL(createUrl(BASE_URL+COLLECTOR_LIST_ENDPOINT,parameters));     
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
      conn.setDoOutput(true);
      conn.setRequestMethod("POST"); 
      conn.setRequestProperty("Content-Type", "application/json"); 
      conn.setRequestProperty("Authorization", "bearer "+p_sAuthToken);



     String input ="{\"survey_id\":"+ p_sSurveyID+",\"fields\":[\"url\"]}"; 
     OutputStream os = conn.getOutputStream();
     os.write(input.getBytes());
     os.flush();
     if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
     throw new RuntimeException("Failed : HTTP error code : " +conn.getResponseCode()); 
      }
     BufferedReader br = new BufferedReString input ="{\"survey_id\":"+ p_sSurveyID+",\"fields\":[\"url\"]}"; 
     OutputStream os = conn.getOutputStream();
     os.write(input.getBytes()); os.flush();
     if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
     throw new RuntimeException("Failed : HTTP error code : " + 
     conn.getResponseCode()); 

      }
      BufferedReader br = new BufferedReader(new InputStreamReader( (conn.getInputStream())));



    conn.disconnect();
    ader(new InputStreamReader( (conn.getInputStream()))); 
    conn.disconnect();
mawia
  • 9,169
  • 14
  • 48
  • 57
  • Hi thanks very much for you answer with String input ="{\"survey_id\":"+ p_sSurveyID+",\"fields\":[\"url\"]}"; – user2993147 Nov 15 '13 at 21:15
  • Hi thanks very much for you answer with String input ="{\"survey_id\":"+ p_sSurveyID+",\"fields\":[\"url\"]}"; getting following error message {"status": 3, "errmsg": "Value 45931029 for field 'survey_id' is not of type string"} – user2993147 Nov 15 '13 at 21:34
  • . . private final static String SURVEY_ID = "12345678"; . . . public static void main(String[] args) { getSurveyList(APP_ID, "myaccesstoken")); getCollectorList(SURVEY_ID, APP_ID, "myaccesstoken")); } . . . public static String getCollectorList(String p_sSurveyID, String p_sApiKey, String p_sAuthToken){ String output=""; System.out.println("in Collectors list"); System.out.println("p_sSurveyID --- " + p_sSurveyID); – user2993147 Nov 15 '13 at 21:41
  • System.out.println("p_sSurveyID --- " + p_sSurveyID); try { List parameters = new ArrayList(); parameters.add(new NameValuePair("api_key",p_sApiKey)); URL url = new URL(createUrl(BASE_URL+COLLECTOR_LIST_ENDPOINT,parameters)); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Authorization", "bearer "+p_sAuthToken); – user2993147 Nov 15 '13 at 21:42
  • String input ="{\"survey_id\":"+ p_sSurveyID+",\"fields\":[\"url\"]}"; OutputStream os = conn.getOutputStream(); os.write(input.getBytes()); os.flush(); if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) { throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); } BufferedReader br = new BufferedReader(new InputStreamReader( (conn.getInputStream()))); conn.disconnect(); -- exception handling } return output; } – user2993147 Nov 15 '13 at 21:46
  • Hi String input ="{\"survey_id\":"+ p_sSurveyID+",\"fields\":[\"url\"]}"; -- should work here the survey_id is getting replaced with the value but its considering as a String. – user2993147 Nov 18 '13 at 21:58