Some time ago, and the company's an Android programmer to develop a simple app, because the first write, time is also tense, the total feeling of writing is not so perfect, take advantage of this period of time to summarize the experience.
For app requests, the type of data returned by the server is typically JSON and XML.
The difference between XML and JSON:
The readability aspect--xml is superior. Because it is made up of a number of nodes, the name of the node can be customized.
The--json of generating data is superior. In PHP to generate a JSON data, only need a built-in function can be implemented, and XML will need to splice strings or instantiate objects to be able to implement, so JSON is more convenient.
The transfer speed aspect-or JSON-dominated. In a nutshell, the length of the string that generates the JSON is much smaller than the length of the generated XML string, and of course, the smaller the string, the better the data transfer.
Format of the communication data:
Code status code such as: 200 for success, 400 for failure, these are customizable.
Message tip information such as: Phone number is incorrect, and so on.
Data information returned by
Code cases that encapsulate the communication interface in JSON way:
<?phpclass response{/************************************ returns JSON formatted data * $param integer $code * $param string $message * $param array $data *return json*******************************/public static function json ($code, $message = ", $data = Array ()) {//To make a validation, if not a number, to return an empty if (!is_numeric ($code)) {return ';} Put the three values that come in, assemble them into an array of $result=array (' Code ' = $code, ' message ' + $message, ' data ' = $data);//json_encode () Convert the array into JSON format output echo json_encode ($result); exit;}? >
In short, the JSON way to encapsulate the communication interface is very simple, XML, it is relatively troublesome.
Small Ant Learning App Interface Development (1)--json-Way Encapsulation Communication interface