Skip to content

Примеры использования

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 -пустые.

Как использовать API (пример для java)

         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");
	}