Teach you to write the basic architecture of the Android network framework.

Source: Internet
Author: User

Teach you to write the basic architecture of the Android network framework.

For more information, see [Mr. Simple blog ].

I am participating in the blog star. Click here to vote for me. Thank you ~ Preface some time ago, I accidentally participated in the selection of the blog star and accidentally entered the Development Group of the famous bloggers Hongyang and Ren yugang. I felt a strong atmosphere of technical discussion, so I came up with the idea of writing some series of blogs. Although I have a limited level, I also hope my blog can help some people who need help. If you need to be a master, it is obviously not suitable for you, so there is no need to look at it again. If you do not know much about framework development or Android network requests, you may need to search Baidu every time you use the network. In the development process, the network is an important part of us, so we started with the network framework or network module. In this framework development process, I will sort out the development ideas and how to consider and solve some design problems. Of course, this is my personal point of view, you can also implement it yourself. In addition to the network framework, the subsequent series also want to update the ImageLoader framework and ORM framework. If there is time, they will also add a series of articles on the animation framework and Weibo development. Of course, these frameworks are only the basis of some simple frameworks. I have a limited level and time, and there are already many ready-made and mature frameworks, here we just learn the wheel building process by repeating the wheel building attitude, so that we can build the wheel. As for many details, we will discuss them too much here. If you are interested, you can study them on your own. Finally, we will name this framework Simple_Net_Framework for the moment. Let's enter the topic together. Basic Structure 1 (basic structure of Simple_Net_Framework) the basic structure of the SimpleNet framework is similar to Volley, and some names are also consistent with Volley. It is mainly divided into four parts, the top part is Request, that is, various Request types. For example, if the returned data type is json, corresponding to JsonRequest, and the returned data string is StringRequest, if you need to upload a file, you need to use MultipartRequest. This request only supports uploading small files, OOM is generated if the uploaded file is too large. The second part is the message queue, which maintains the list of requests submitted to the network framework and sorts the requests according to the corresponding rules. By default, the queue is executed with a higher priority and the order in which it enters the queue. This queue uses the thread-safe PriorityBlockingQueue <E> because our queue will be accessed concurrently, therefore, we need to ensure the atomicity of access. The third part is Executor, which is the Executor of the network. The Executor inherits from the Thread and cyclically accesses the second part of the Request queue in the run method. After the request is completed, the result is delivered to the UI Thread. In order to better control the Request queue, such as request sorting and cancellation operations, we did not use the Thread pool to operate here, but managed the queue and Thread form on our own, in this way, the entire structure becomes more flexible. The fourth part is the Response shipping class. In the third part, the Executor executes network requests. The Executor is a Thread, but we cannot update the UI in the main Thread, so we use

ResponseDelivery is used to encapsulate the delivery of Response and ensure that Response is executed in the UI thread.

Each part has a relatively single responsibility to facilitate future upgrades and maintenance. In framework analysis, figure 1 looks like a layered architecture. In fact, it is not. This diagram expresses its logical order rather than structure. In our application development, layered architecture is an important means, as shown in figure 2. Figure 2 however, in the development process, we often couple the UI and the business layer because they are too closely related and are not so easy to break down. Experts can simplify complex things, and decomposition is an important means of simplification. The process of decomposition becomes a reconstruction in the development process. However, I also want to learn how to separate the UI and business layer. If you have a good solution, I hope you can give me more advice. Then we introduce a layered concept. To facilitate understanding, you can also follow the 1 structure to deepen understanding. So what are the advantages and disadvantages of layering? Advantages:

1. Simplify the decomposition of complex problems. Each layer is responsible for its own implementation and provides external services;

2. separation of duties. Many people develop complex systems. Management and integration of these functions is a serious problem. After implementation of hierarchical design, each layer only needs to define its own external interface, and other dependent layer services can be developed;

3. Each layer is independent of other layers, and the Implementation Details are hidden from the outside. The upper layer does not need to know the details of the lower layer, but only needs to call the interface;

4. It is conducive to standardization.

Disadvantages:

1. Modifications to the business in the domain may need to be modified at multiple layers after layering;

2. Too many layers affect performance.


As mentioned above, our Simple_Net_Framework is not hierarchical, but simple and modular, but its theoretical basis is similar. It relies on abstraction rather than implementation and a single responsibility ...... the concept of hierarchy is introduced here, which is easy to understand. It is also hoped that the module cohesion and coupling will be ensured as much as possible during the development process. Let's look at Simple_Net_Framework. Request is an abstract generic class, and the generic type is the Response type returned. For example, StringRequest is inherited from Request <String>. The RequestQueue in the second part depends on the Request and the Request is abstract. Therefore, any subclass of the Request can be passed to the Request queue. It depends on the abstract Request rather than a specific implementation, this guarantees scalability. You can implement your own Request, such as the upload Request for large files. Similarly, the NetworkExecutor in the third part only relies on Request abstraction. However, an HttpStack type is introduced here. The real executors of this network Request include HttpClientStack and HttpUrlConnStack, the two are HttpClient of Apache and HttpURLConnection of java respectively. For the differences between the two, see HttpURLConnection or HttpClient for Android network access ?. HttpStack is also an abstraction. Whether HttpClient or HttpURLConnection is used depends on the running system version. HttpStackFactory returns the corresponding HttpStack to the framework based on the system version. The final ResponseDelivery is relatively simple. It only ships the result to the UI thread through Handler for execution, that is, to execute the onComplete method of RequestListener. At this time, the network execution is complete, you can update the UI or other related operations in this method. Next let's look at the engineering structure of SimpleNet, as shown in 3. Figure 3 Figure 4 This is the basic structure of the Simple_Net_Framework framework. If you are looking forward to the next blog update, please submit it to the top! Thank you ~ I am participating in the blog star. Click here to vote for me. Thank you ~

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.