Android Network Framework Principles

Source: Internet
Author: User

Here's a summary of the general principles of an Android network framework:

HTTP Network Request principle

Learned the "computer network" should all know that HTTP is an application layer protocol, it realizes the reliable data transmission through TCP, can guarantee the integrity and correctness, and TCP for the advantages of data transmission control can also be reflected in HTTP, make HTTP data transmission throughput, efficiency is guaranteed.

For mobile development, network applications are basically c/s architecture, that is, client/server architecture. The client initiates a specific request to the server, the server returns the result, the client parses the result, and the results are displayed on the UI.

The detailed interaction process has the following steps:

The client performs a network request and resolves the server's hostname from the URL

Converts the server's host name to the server's IP address

Resolve the port number from the URL

Establish a TCP connection between the client and the Web server

The client sends an HTTP request through the output to the server

The server sends an HTTP response message to the client

The client gets the message from the input stream

Client parsing message, closing connection

The client displays the results on the UI

There are two ways to execute a network request in Android: HttpClient, HttpURLConnection, and in fact the network framework is mainly based on this to do some function expansion and encapsulation.

Producer Consumer Model

I was looking at Java concurrency in the morning and found that, in fact, the generic Android
The network framework is a typical producer-consumer model. I don't know much about the service side, and I think that the server, when processing client concurrent requests, also
Corresponds to a typical producer-consumer model. (Just thinking, not too mature). This content will not be detailed here, and subsequent blogs will have a special topic to explain concurrency related content.

The client requests multiple MOBILEAPI, corresponding to multiple tasks, and then multiple tasks go into a buffer, and multiple network-executing threads will process the tasks in this buffer concurrently. A typical producer-consumer model.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/

Blockingqueue can be used to act as a shared memory buffer for maintaining the request task queue.

Blockingqueue is similar to a "suggestion box". It is suitable as the channel of data sharing, is a Java and contract in a more important class, how to achieve the specific people can go down research. I'm not going to talk about it here. It is often used in conjunction with Threadpoolexecutor.

Blockingqueue is a good choice for producers and consumers to achieve. But Blockingqueue is not a high-performance implementation. I'm still in the study, and this is the place to start.

Command mode

The feature of command mode is to decouple the command sender from the command recipient, in addition, the command mode can add a queue of commands to support the revocation of the command. This model clearly corresponds to the Android network framework one by one, and the Android Network framework also supports the revocation of commands.

Here does not explain the command mode, the previous design Pattern series blog has already talked about this piece of content, you can go to look at the previous blog.

Support Extensions

What are the possible extensibility orientations for the Android network framework? There should be requests that can support multiple formats, such as String, XML, JSON, and so on. How to design the extensibility of this block, the design of each network frame converge to the same.

Asynchronous and callback (Callback)

The network request must be asynchronous, but after the execution of the network request, you need to tell the asynchronous task performer the results of the execution, and then you need a callback. But the callback is only a scheme.

More information:

Http://www.ruanyifeng.com/blog/2012/12/asynchronous%EF%BC%BFjavascript.html

http://quinnhe.iteye.com/blog/2192940

http://www.jianshu.com/p/3141d4e46240

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.