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.

Khaled Lela
  • 7,831
  • 6
  • 45
  • 73

2 Answers2

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
Tomek
  • 557
  • 2
  • 7
  • 24
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();
    }
}
Khaled Lela
  • 7,831
  • 6
  • 45
  • 73