The Android client uses the Http Post request to interact with the server.

Source: Internet
Author: User

The Android client uses the Http Post request to interact with the server.

This example uses Servlet as an example to demonstrate the communication between Android and Servlet..

As we all know, Android and servers usually use HTTP and Socket communication methods, while HTTP Communication methods are divided into get and post methods. Socket communication will be introduced in a later blog.

HTTP Protocol Introduction:

HTTP (Hypertext Transfer Protocol) is the foundation of Web networking and one of the common protocols for mobile phone networking. HTTP is a Protocol built on TCP.

The most notable characteristic of HTTP connection is that each request sent by the client requires a server to send a response. After the request ends, the connection is released. The process from establishing a connection to closing a connection is called "One connection ". In HTTP 1.0, each client request requires a separate connection. After processing the request, the connection is automatically released. In HTTP 1.1, multiple requests can be processed in a single connection, and multiple requests can overlap. You do not need to wait for a request to end before sending the next request.

Because HTTP actively releases connections after each request ends, HTTP connections are short connections and stateless, so you must keep the client program online, you must constantly initiate connection requests to the server. The common practice is to send a "keep connection" request to the server at regular intervals even if no data is required, the server replies to the client after receiving the request, indicating that the client is "online ". If the server cannot receive a request from the client for a long time, the client is considered to be "offline". If the client cannot receive a response from the server for a long time, the network is considered disconnected.

Each time a client based on HTTP1.0 sends a request to the server, the server returns a Response Message to the client. After confirming that the client has received the response message, the server closes the network connection. During this data transmission process, no historical information and status information are stored. Therefore, HTTP is also considered stateless.

The biggest difference between HTTP1.1 and HTTP1.0 is that persistent connections are supported. After the client connects to the server using the HTTP1.1 protocol, the server returns the initiative of closing the client connection to the client. That is to say, as long as you do not call the close method of the Socket class to close the network connection, you can continue to send HTTP requests to the server.

The HTTP connection uses the "request-response" method (two handshakes). Not only do you need to establish a connection during the request, but after the client sends a request to the server, the server can only reply to Data. The Socket connection can directly transmit data after both parties establish a connection.

HTTP features:

Supports the B/S and C/S modes;

Simple and fast: when a customer requests a service from the server, only the Request Method and path are required. Common Request methods include GET, HEAD, and POST.

Flexible: HTTP allows transmission of any type of data objects. The Type being transferred is marked by Content-Type;

Stateless: HTTP is stateless. Stateless means that the Protocol has no memory for transaction processing. The lack of status means that if subsequent processing requires the previous information, it must be re-transmitted, which may increase the amount of data transmitted each connection.

HTTP Request Method:

The request line contains the request method, which is explained as follows:

GET Request to obtain the resource identified by Request-URI;

POST attaches new data to the resource identified by Request-URI;

HEAD Request to obtain the Response Message Header of the resource identified by Request-URI

The PUT Request server stores a resource and uses Request-URI as its identifier.

The DELETE Request server deletes the resource identified by Request-URI;

TRACE Request information received by the server for testing or diagnosis

CONNECT reserved for future use

OPTIONS requests query server performance, or query resource-related OPTIONS and requirements

 

Difference between Get and Post requests:

THE Post request can send data to the server and put the data in the html header together to the server URL address. The data is invisible to the user. Get is to add the parameter data queue to the submitted URL, the value and each field in the form one-to-one correspondence, for example (http://www.baidu.com/s? W = % C4 & inputT = 2710)

The data size transmitted by get is small and cannot exceed 2 kb. The amount of data transmitted by post is large, which is generally not restricted by default. Theoretically, the maximum size of IIS4 is 80 KB, and that of IIS5 is 100KB.

Get has low security and high post security.

Project Structure:

 

Get method:

The get mechanism uses? And then pass the parameter to the client in the form of name = value. Therefore, AndroidGetTest must be implemented in the Android project first. in java, the onCreate method defines the URL address and parameters to be passed, and then opens an HttpURLConnection link through the URL. This link can obtain the InputStream byte stream object, it is also an important process for outputting data to and from the server, and if the server response. getInputStream. when write () returns information to andorid, it can be converted using InputStreamReader to display the returned data with BufferReader.

The Code is as follows:

The Servlet receives data and returns a notification:

 

Android sends a message and receives the message returned by the Servlet:

Post method:

The post transmission method is not transmitted in the URL, but also solves a series of problems such as small get transmission volume, easy tampering, and uneasiness. Mainly through

After setting HttpURLConnection, let it support the post transmission mode, and then pass parameters through the relevant properties (if you need to pass Chinese characters, you can use URLEncoder encoding, you can use URLDecoder to decode the data at the retrieval end)

 


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.