Small Ant Learning App Interface Development (1)--json-Way Encapsulation Communication interface

Source: Internet
Author: User

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:

    1. The readability aspect--xml is superior. Because it is made up of a number of nodes, the name of the node can be customized.

    2. 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.

    3. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.