"Android Notes" Android action HTTP implementation communicates with server

Source: Internet
Author: User
Tags html header

This example demonstrates the communication between Android and the servlet, using the servlet as an example.

As we all know, the communication between Android and server usually uses HTTP communication mode and socket Communication mode, and HTTP communication method is divided into get and post two kinds of ways. As for the socket communication will be introduced in later blog post.

Introduction to the HTTP protocol:

HTTP (Hypertext Transfer Protocol), which is the foundation of Web networking and one of the most commonly used protocols for mobile networking, is a protocol built on the TCP protocol.

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

Because HTTP is actively releasing the connection after each request ends, the HTTP connection is a "short connection", "stateless", and to keep the client program online, a connection request needs to be made to the server continuously. The usual practice is that even if you do not need to obtain any data, the client also keeps sending a "keep-connected" request to the server at regular intervals, and the server responds to the client after receiving the request, indicating that the client is "online". If the server can not receive the client's request for a long time, it is considered that the client "offline", if the client cannot receive a reply from the server for a long time, it is considered that the network has been disconnected.


The HTTP1.0 protocol-based client returns a response message to the client each time a request is made to the server, and the server shuts down the network connection after confirming that the client has received a response message. In this data transfer process, no historical information and status information is saved, so the HTTP protocol is also 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 of shutting down the client connection to the client, which means that the HTTP request can continue to be sent to the server, as long as the close method of the socket class is not called to close the network connection.

The HTTP connection uses the "request-response" approach (2 handshake), not only to establish a connection at the time of the request, but also to request the server to reply to the server. The socket connection can transmit data directly after the connection is established.

Features of the HTTP protocol:
Support b/S and C/s mode;

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

Flexible: HTTP allows the transfer of any type of data object. The type being transmitted is marked by Content-type;

Stateless: The HTTP protocol is a stateless protocol. Stateless means that the protocol has no memory capacity for transactional processing. A lack of state means that if the previous information is required for subsequent processing, it must be re-routed, which may cause the amount of data to be transferred per connection to increase.

HTTP protocol Request method:

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

Get request gets the resource identified by Request-uri;

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

HEAD request Gets the response message header for the resource identified by Request-uri

PUT Request server stores a resource and uses Request-uri as its identity

Delete Requests the server to remove the resources identified by the Request-uri;

TRACE requests the server to echo received request information, primarily for testing or diagnostics

CONNECT reserved for future use

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


Get differs from POST request:

The POST request can transmit data to the server, and the data is sent to the service-side URL address together with the HTML header, and the data is not visible to the user. And get is to add parameter data queue to the URL of the submission, the value and the form within the field one by one corresponding, for example (http://www.2cto.com/s?w=%C4&inputT=2710)

Get transmits a small amount of data and cannot be greater than 2KB. Post transmits a large amount of data, which is generally not restricted by default. In theory, however, the maximum amount of IIS4 is 100KB in 80KB,IIS5.

Get security is very low and post security is high.

Get Way:
Get mechanism is used in the URL address through? And then pass parameters to the client in the form of Name=value. So first of all, in the Android Project Androidgettest.java OnCreate method defines its URL address and parameters to be passed, and then open a httpurlconnection link through the URL, this link can be obtained inputstream byte stream object, also is an important process of outputting and returning data from the server to the server, and if the server Response.getInputStream.write () returns information to Andorid, it can be converted by InputStreamReader. The returned data is displayed with Bufferreader.
The specific code is as follows:
The servlet side receives the data and returns the notification:

Post Way:
The post transmission method does not pass in the URL, but also solves a series of deficiencies, such as small get transfer volume, easy tampering and insecurity. Mainly through the
The httpurlconnection is set to support the post transmission mode, and then pass the parameters through the relevant properties (if you need to pass the Chinese characters, you can pass the Urlencoder encoding, and the acquisition end with Urldecoder decoding can be)

Reference Original: http://www.2cto.com/kf/201203/123283.html

"Android Notes" Android action HTTP implementation communicates with server

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.