Android HTTP Implementation and server communication

Source: Internet
Author: User
Tags html header requires socket

Android communicates with the server in the form of HTTP communication and socket communication, while HTTP communication methods are two ways of Get and post. As for socket communication will be introduced in later posting.

Introduction to HTTP protocol:

HTTP (Hypertext Transfer Protocol) is the foundation of Web networking and one of the most common protocols in mobile networking, and HTTP protocol is a protocol based on TCP protocol.

The most notable feature of HTTP connections is that each request sent by the client requires a server loopback response, and the connection is actively released after the request is completed. The process from establishing a connection to closing a connection is called a "once connection." In HTTP 1.0, each request from the client requires a separate connection to be established, and the connection is automatically released after the request has been processed. In HTTP 1.1, you can handle multiple requests in a single connection, and multiple requests can overlap, without waiting for a request to end before sending the next request.

Because HTTP actively frees the connection at the end of each request, the HTTP connection is a "short connection" and "stateless", and to keep the client program online, you need to continually initiate a connection request to the server. The usual practice is that even if you do not need to obtain any data, the client also maintains a fixed period of time to send a "stay Connected" request to the server, the server after receiving the request to the client to reply, indicating that the client "online." If the server is unable to receive the client's request for a long time, the client is considered "offline" and if the client cannot receive a reply from the server for a long time, the network has been disconnected.

A client that is based on the HTTP1.0 protocol returns a response message to the client after each request is made to the server, and the server closes the network connection after confirming that the client has received a response message. In this data transfer process, no historical information and state information are preserved, so the HTTP protocol is considered a stateless protocol.

The biggest difference between HTTP1.1 and HTTP1.0 is the increased support for persistent connections. When the client connects to the server using the HTTP1.1 protocol, the server returns the initiative to turn off the client connection to the client; that is, you can continue to send HTTP requests to the server as long as you do not call the Close method of the socket class to turn off the network connection.

The HTTP connection uses a "request-response" (2 handshake), not only to establish a connection at the time of the request, but also to require the client to send a request to the server before the server can reply to the data. The socket connection can be transmitted directly after the two sides have established a connection

Features of the HTTP protocol:

Support b/S and C/s mode;

Simple and quick: When a client requests a service from a server, it simply transmits the request method and path. The request method commonly has, POST.

Flexibility: HTTP allows data objects of any type to be transmitted. The type being transmitted is marked by Content-type;

No status: HTTP protocol is stateless. Stateless means that the protocol has no memory capability for transaction processing. A lack of status means that if the preceding information is required for subsequent processing, it must be retransmission, which may result in an increase in the amount of data transmitted per connection.

HTTP protocol Request method:

The request method is included in the request line, as explained below:

The GET request obtains the resource identified by the Request-uri;

Post appends the new data to the resource identified by the Request-uri;

The head request gets the response message header of the resource identified by Request-uri

The put request server stores a resource and uses Request-uri as its identity

Delete requests that the server delete the resource identified by the Request-uri;

TRACE requests that the server echo the requested information received, primarily for testing or diagnostics

CONNECT reserved for future use

Options request query server performance, or query resource-related choices and requirements

Get differs from POST request:

Post requests can transmit data to the server, and the data is delivered to the service-side URL in the HTML header, and the data is not visible to the user. And get is to add the parameter data queue to the submitted URL, and the value corresponds to the field one by one in the form, for example (http://www.baidu.com/s?w=%C4&inputT=2710)

The amount of data transferred by get is small and cannot be greater than 2KB. Post transfers have a large amount of data, which is generally default to unrestricted. In theory, however, the maximum number of IIS4 is 100KB in 80KB,IIS5.

Get security is very low, post security is high.

Project Structure Chart:

Get Way:

The get mechanism uses the number interval in the URL address, and then passes the parameters to the client in the form of Name=value. So first of all, in the Androidgettest.java under the Android project, the OnCreate method defines its URL address and the parameters to be passed, and then opens a httpurlconnection link through the URL, which can get InputStream byte stream object and is an important process of outputting data to the server and returning it from the server, and if the service-side response.getInputStream.write () returns information to Andorid, it can be converted by InputStreamReader. Displays the returned data in Bufferreader.

The specific code is as follows:

The servlet side receives the data and returns a notification:

The Android side sends the message and receives the message that the servlet returns:

Post method:

Post transmission mode is not passed in the URL, also just solve the get transmission volume is small, easy to tamper with and unsafe a series of deficiencies. is mainly through

HttpURLConnection settings, so that it supports post transmission, and then pass the parameters through the relevant properties (if you need to pass the Chinese characters, you can pass the Urlencoder code, and the acquisition end using Urldecoder decoding)

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.