-
Notifications
You must be signed in to change notification settings - Fork 0
Примеры использования
khavrenko edited this page Mar 15, 2012
·
2 revisions
Вызов функции
method : some_action,
params : {
some_params
}
Пример
{"method":"activate","params":{"id":10}}
Возвращаемый результат
{"result":some_result,"error":{"info":some_info,"code":some_code}}
Если ошибок не произошло, то error.info и error.code -пустые.
JSONObject jsonData = new JSONObject();
//you can use method 'put' to add data into json object
jsonData.put(parameter, value);
//when you 'put' all parameters into jsonData pass in function sendPOST this
"data="+jsonData.toString()
// second argument of sendPOST must look like this : "data={\"action\":\"loadData\"}"
//function to send json in POST
public String sendPOST(String url, String jsonData) throws IOException {
//create connection
URLConnection conn = new URL(url).openConnection();
//Set the DoOutput flag to true, because we intend to use the URL connection for output
conn.setDoOutput(true);
//create OutputStreamWriter
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "ASCII");
//write json data into out stream
out.write(jsonData);
out.write("\r\n");
//clear buffer
out.flush();
//close stream
out.close();
//return response from the server
return readStreamToString(conn.getInputStream(), "UTF-8");
}