Explain the interaction between Android client and server in detail

During the development of recent Android projects, a problem has plagued me for a long time. There are several ways for Android clients to interact with servers, the most common of which are web services and JSON. To interact with PC server in Android mobile client, the following conditions need to be met: cross platform, standard transmission data format and convenient interaction.

In order to communicate with the server, in fact, it is nothing more than two protocols: HTTP and TCP. Learn about socket for TCP. If HTTP is used, get familiar with HTTP protocol and related Java APIs. The following methods are extended from these two protocols: WebServices soap, SSH JSON (refer to this link), XMLRPC (WordPress for Android)

Socket is not recommended. HTTP restful is recommended. When transmitting data to the server, the restful API is generally used. In this way, we must first have a preliminary understanding of the HTTP protocol, and at least know what the get / post is and what the difference is.

If there are no special requirements, using web services to transmit XML files is relatively simple and general. If there are requirements for data size and transmission speed, JSON is more suitable.

[difference between socket and HTTP connection]

The HTTP connection uses the "request response" method. It not only needs to establish a connection when requesting, but also needs the client to send a request to the server before the server can reply to the data.

Socket can listen, so once the socket connection is established, the communication parties can start sending data content to each other until the connection is disconnected. Maintain real-time and synchronization of client and server data.

XML RPC is an RPC mechanism that uses HTTP protocol as the transmission protocol and uses XML text to transmit commands and data.

RPC is the abbreviation of remote procedure call, which translates into Chinese as remote procedure call. It is a technology to call a procedure (method) on the remote machine on the local machine. This process is also known as "distributed computing"

There are many examples on the Internet to demonstrate how Android client and server data interact. However, most of these examples are complicated, which is unfavorable for beginners. Now we introduce an interactive example with simple code and clear logic:

Transfer XML files using WebServices:

1、 Server side:

Code 1: add a file named "androidserverservlet. Java"

Code 2: modify the file named "web. XML"

2、 Android mobile client:

Code 1: add a file named "androidclientactivity. Java"

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>