Several common asynchronous frameworks and network access frameworks are differentiated and compared.
Part1:
In our program, some time-consuming tasks are not allowed to appear in the main thread, mainly to prevent the Anr (Application not Responding) caused by blocking the main thread ), some time-consuming tasks mainly include:
Network Access, slow disk operations, and time-consuming Algorithms
When the processing of an event by our main thread exceeds a certain period of time, the main thread will crash and report ANR,
Common Solution: Use the sub-Thread Technology to remove time-consuming tasks from the main thread
1. handler Mechanism
You only need to send the UI update parameters to the handleMessage in the defined Handler using sendMessage in the Child thread to update the UI in the main thread (Handler implements the jump from the Child thread to the main thread)
2. runOnUiThread Method
This method allows the current main thread to obtain cpu resources for UI updates (as to how to return data from the Child thread, there are many methods, such as calling the interface back and forth to obtain parameters)
3. Use the familiar AsyncTask class
AsyncTask usage and source code analysis
Part2: Network Access
You are familiar with the right HttpClient and HttpUrlConnection methods, including Volley, OkHttp, and AsyncHttpClient.
For the first two basic methods, asynchronous processing is not available, that is, we need to use them with the asynchronous Processing Framework in part1, otherwise, you will be able to accept the naked embarrassment of ANR.
Volley is capable of asynchronous access and the callback Method for access termination is in the main thread, in this way, you can directly use it independently from the asynchronous access framework (and replace the Universal-Image-Loader asynchronous loading of images) Volley Usage Details
AsyncHttpClient is an asynchronous encapsulation of HttpClient. One drawback of Volley is that the callback method is still in the Child thread. We still need to use the asynchronous framework in part1 to solve the problem.