Question

I am trying to send the following data to server

{"feedback":{"q1":12.0, "q2":33, "q3":12.0, "q4":78, "q5":12.0, "q6":33, "q7":12.0, "q8":33, "q9":12.0, "q10":33, "latitude":12.0, "longitude":33.08, "imei":128790, "time":"base64string"}}

I am unable to create a proper jsoup String. i am able to connect to my server but when i try to send the following information using POST request. it fails.

I tried to send the same request through my WEB CONSOLE, and it worked fine there as well.

Below is my code:

      protected String doInBackground(String... urls) {

                try {
                    // Connect to the web site
                    Document document = Jsoup.connect("http://107.170.91.100/books/save_feedback")
                            .data("q1", q1).data("q2", q2).data("q3", q3).data("q4", q4).data("q5", q5).data("q6", q6).data("q7", q7).data("q8", q8)
                            .data("q9", q9).data("q10", q10)
                            .data("latitude", "234").data("longitude", "344.4").data("imei", ime)
                            .data("time", MainActivity.b64)
                            .post();


                    //String title = document.title();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;

        }

What am i missing here? i guess the format of my data in which i am sending is incorrect. do i need to add the word feedback in the data as well? Any help would be appreciated

Was it helpful?

Solution

If the server expects a JSON string, try this instead:

// Build the JSON string...
String format = "{\"feedback\":{\"q1\":%s, \"q2\":%s, \"q3\":%s, \"q4\":%s, \"q5\":%s, \"q6\":%s, \"q7\":%s, \"q8\":%s, \"q9\":%s, \"q10\":%s, \"latitude\":%s, \"longitude\":%s, \"imei\":%s, \"time\":\"%s\"}}";
String jsonData = String.format( //
           format, //
           q1, q2, q3, q4, q5, //
           q6, q7, q8, q9, q10, //
           "234", "344.4", //
           ime, time //
);

// Now send it...
Document document = Jsoup
                    .connect("http://107.170.91.100/books/save_feedback")
                    .data("JSON", jsonData) // The parameter name may not be JSON, change accordingly
                    .post();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top