В основном storeAnswer
должно быть post
запросом и payload
, которое будет добавлено к запросу body
в формате json
.
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();
}
}