Mobile development, or most of other software development, our programmers do nothing more than three things: getting data, displaying data, getting user events, and making corresponding feedback. In addition, due to the unstable network environment in mobile development, obtaining network data has become a bottleneck for many applications. This article will try to build a stable, efficient, and universal mobile development communication framework.
This article is based on the j2_environment and can be easily transplanted to other platforms.
The following is a step-by-step analysis based on my ideas:
1. Anyone who has worked in mobile development knows that communication will be completed in a separate thread. Do not explain the reason ......
2. Communication?: Request (Request, StructRequest), send a Request, get a Response (HttpHandler, HttpClient), and then get the Response (Response, StructResponse)
3. There are only two communication results: Success and Failure. If the communication is successful, the UI thread (HttpListener) should be notified to complete data painting. If it fails, there will be more situations. As a saying goes, there will be one beauty, but each ugly one has its own ugliness. In most cases, communication failures can be detected by HttpListener and fed back to the UI thread. But what if there is no response? Where is the program stuck? Obviously not. At this time, a timer listener (HttpMonitor) is required. When the communication times out, the communication will be abandoned.
The following shows the class diagram of the entire framework (I think the class diagram is ugly and cannot be used ......)
The first article is written here, giving you an overall impression. The next article analyzes the request block and uploads the source code.
Author: "Feng Xiaowei"