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\"}]"
问题在&answers=
之后,无法识别为url的一部分,
格式为JsonObject
的字符串。
答案 0 :(得分:1)
答案 1 :(得分:0)
基本上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();
}
}