Teach you the basic architecture of the Android network framework

Source: Internet
Author: User

Reprint Please indicate the source, this article from "Mr.simple's Blog".

I am participating in the blog star, click here to vote for me, thank you ~Prefacein the previous period of time, accidentally participated in the selection of the star of the blog, also accidentally into the Hong Yang and Ningyugang two well-known bloggers of the development group, felt a very strong technical discussion atmosphere, so he also appeared to write some of the idea of a series of blog. Although I have limited level, but also hope that their own blog can help some people who need help. Need you to be a master, then obviously not suitable for you, there is no need to look at it anymore. If you do not understand the framework development or the Android network request, every time you want to use the network to Baidu search, then it may be you need. In the development process, the network is a very important part of us, so we start with the network framework or network module. In this framework development process, I will tidy up the development of ideas, and encounter some design problems will have what kind of thinking, solutions, of course, this is only my personal opinion, we can also have their own implementation. In addition to the network framework, the following series also wants to update the Imageloader Framework, ORM Framework, and, if there is time, a series of articles on animation frames and Weibo development. Of course, these frameworks are just some simple framework basis, I have a limited level of time, and already have a ready, mature many frameworks, we are here just to repeat the wheel of the attitude to learn the wheel construction process, so as to be able to build the point of the wheel. As for the many details, we have discussed the issue here, and if you are interested, you can study it on your own. Finally, let's name the framework simple_net_framework, let's go into the topic. Basic Structure Figure1 (the basic structure of simple_net_framework)the basic structure of the simplenet framework is similar to that of volley, including some names that are consistent with volley. ItIt is divided into four parts, the top part is request, which is the various types of requests. For example, the return data type of JSON corresponds to Jsonrequest, return the data string for the stringrequest, if you need to upload a file, then you need to use Multipartrequest, the request only support small file upload, If the uploaded file is too large, it will cause oom.The second part is Message Queuing, which maintains a list of requests submitted to the network framework and sorts them according to the corresponding rules. By default it is more prioritized and queued in order to execute, the queue uses thread-safe priorityblockingqueue<e&gt, because our queues are accessed concurrently, so we need to guarantee the atomicity of access.The third part is executor, which is the performer of the network. The executor inherits from thread, iterates through the second part of the request queue in the Run method, and posts the result to the UI thread after the request is complete. In order to better control the request queue, such as request ordering, cancellation, and so on, we do not use the thread pool to operate, but rather self-management queue and thread form, so that the entire structure becomes more flexible.Part IV is the response delivery class, which executes the network request in the third part of the executor, executor is thread, but we are not able to update the UI in the main thread, so we use

Responsedelivery to encapsulate response delivery, guaranteeing response execution on the UI thread.

Each part of the responsibility is relatively single, so as to facilitate future upgrades and maintenance.Framework AnalysisFigure 1 looks a bit like a layered architecture, not really, it's more about expressing its logical order than structure. In our application development, layered architecture is an important tool, as shown in 2.Figure 2But in the development process, we tend to couple the UI with the business layer, because they are so closely related that it's not so easy to break up. A master can simplify complex things, and decomposition is an important means of simplification, decomposition of the process in the process of development we become a refactoring. But how to separate the UI and business layer is also I recently want to learn, if you have a good solution, but also hope to advise.Then we introduce a layered concept that you can use to understand 1 of the structure. So what are the pros and cons of layering?Advantages:

1. complex problem decomposition simplification, each layer is responsible for their own implementation, and provide services to the outside;

2, separation of duties, complex systems have a lot of personnel to develop, these features development of management and integration is a very serious problem, layered design after implementation, each layer only need to define their own external interface, other dependent layer services can be developed;

3, each layer of the other layers are independent, the implementation of the external hidden details, the upper layer without knowing the details of the lower layer, just call the interface can be;

4, in favor of standardization.

Disadvantages:

1, after layering for the domain business changes may need to modify many layers;

2, excessive level of impact performance.


As stated above, our simple_net_framework is not stratified,It's simple modularity,But the theoretical foundations are similar, dependent on abstraction and not on implementation, single duty ... This introduces the concept of layering, which is easy to understand, but also hope that we can in the development process as far as possible to ensure the cohesion of the module, coupled.Look againSimple_net_framework,request is an abstract generic class, and the generic type is the response type that is returned, for example Stringrequest is inherited from Request<string>. The second part of the Requestqueue relies on the request and the requestis abstract, so any subclass of request can be passed to the request queue, which relies on abstract request rather than a specific implementation. Scalability is therefore guaranteed. You can implement your own request, such as uploading a request for a large file. Similarly, the third part of the networkexecutor is only dependent on the request abstraction, but here another type is introduced Httpstack, the real performer of this network request, has Httpclientstack and Httpurlconnstack, respectively, of Apache HttpClient and Java httpurlconnection, For the difference between the two, please refer to: Android access to the network, using HttpURLConnection or httpclient? Httpstack is also an abstract, concrete usehttpclient or HttpURLConnection is set by the operating system version,Httpstackfactory will return the corresponding to the framework according to the system version.Httpstack. The finalResponsedelivery is relatively simple, just by handler the results to the UI thread execution, that is, the execution of Requestlistener OnComplete method, when the network execution is complete, The user can update the UI or other related actions in the method.Let's take a look at the engineering structure of Simplenet, shown in 3. Fig. 3 Fig. 4 This issimple_net_framework framework of the basic structure, if you look forward to the next blog update, please top a post it! Thank you ~ I am participating in the blog star, Click here to vote for me , thank you ~

Teach you the basic architecture of the Android network framework

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.