Pregunta

EDIT: thanks for the suggestions. I changed the code to implement the json object instead of string and sent in the body

This is what I tried to get details from survey monkey. I get the response code as 200 but however couldn't get the data I need. Could some one please let me know where I'm doing this wrong. may be this url can be helpful to specify what I'm trying to do https://developer.surveymonkey.com/mashery/requests_responses this is o/p I get {"status": 3, "errmsg": "No JSON object could be decoded: line 1 column 0 (char 0)"}

    package surveydetails;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class Surveydetails {

        public static void main(String args []) {       
        String url = "https://api.surveymonkey.net/v2/surveys/get_survey_list?api_key=myapikey";                    
                    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.setRequestMethod("POST");


conn.setRequestProperty("Authorization", "bearer myauthtoken"); 
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();
                    }

                }
}
¿Fue útil?

Solución

The POST body's JSON encoding is invalid.

Your code String body=" '{\"fields\":[\"title\"]}'";

should be String body="{\"fields\":[\"title\"]}";

I would suggest using a JSON library like json-simple when encoding JSON objects to avoid invalid JSON. A library will also be more flexible as your application develops.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top