Android HTTP implements communication with the server

Source: Internet
Author: User
Tags html header

This post is reproduced, not prepared by myself.

 

 

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.ProgramTo 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 adds the parameter data queue to the submitted URL, and the values correspond to each field in the form one by one. For example (Http://www.baidu.com? 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.
DetailsCodeAs 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)

 

 

 

Complete. Pai_^

 

 

 

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.