1
http://192.168.0.14/something/storeAnswerGet?longitude=20.49158053365199&latitude=44.798944935485885&answers="[{\"id\":3,\"id_question\":7,\"user_id\":1,\"answer\":\"Beograd\"},{\"id\":3,\"id_question\":7,\"user_id\":1,\"answer\":\"Valjevo\"},{\"id\":3,\"id_question\":8,\"user_id\":1,\"answer\":\"Da\"}]"

Problem is after &answers= it is not recognized as part of url, that is a formatted JsonObject in string.

5
  • You have to convert it to a string and then append it in the url. Commented Jul 6, 2018 at 8:41
  • How do you concatenate the URL string? Commented Jul 6, 2018 at 8:48
  • Thank for your answers, i have solved this problem by placing a string in Uri.encode (jsonStringArray). And URL looks "192.168.0.14/storeAnswerGet?&answers="+ Uri.encode(jsonStringArray) Commented Jul 6, 2018 at 8:53
  • have you try: ..&answers="+URLEncoder.encode(json_here) Commented Jul 6, 2018 at 8:55
  • Uri.encode works fine for me, thanks again Commented Jul 6, 2018 at 9:06

2 Answers 2

1

Explained here: https://stackoverflow.com/a/27578923/1088975

Basically you have 2 options

  1. Send data with POST method
  2. Encode text and send with GET
Sign up to request clarification or add additional context in comments.

Comments

0

Basically storeAnswer shall be post request and payload to be added on the request body with json format.

public void sendAnswerRequest(){
    try {
        URL url = new URL("http://192.168.0.14/something/storeAnswerGet");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
        conn.setRequestProperty("Accept","application/json");
        conn.setDoOutput(true);
        conn.setDoInput(true);

        JSONObject jsonParam = new JSONObject();
        jsonParam.put("latitude", 44.798944935485885);
        jsonParam.put("longitude", 20.49158053365199);

        JSONArray answers = new JSONArray();

        JSONObject answer = new JSONObject();
        answer.put("id",3);
        answer.put("id_question",7);
        answer.put("user_id",1);
        answer.put("answer","Beograd");

        answers.put(answer); // Add all answers to answer array..

        jsonParam.put("answers",answers);

        Log.i("JSON", jsonParam.toString());
        DataOutputStream os = new DataOutputStream(conn.getOutputStream());
        //os.writeBytes(URLEncoder.encode(jsonParam.toString(), "UTF-8"));
        os.writeBytes(jsonParam.toString());

        os.flush();
        os.close();

        Log.i("STATUS", String.valueOf(conn.getResponseCode()));
        Log.i("MSG" , conn.getResponseMessage());

        conn.disconnect();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.