Android Data Request framework

Source: Internet
Author: User

Android Data Request framework

Most Android applications generally involve interaction with servers, except for some single-host applications. Since it is necessary to deal with the server, it is almost necessary to request data from the server. Maybe every APP has its own specific implementation logic, however, we can still Abstract A General Data Request framework.

First:


Let's analyze it in detail. First, the vertical dotted line is the Android client on the left, and the server and background database on the right.

In the left-side figure, the three gray modules are MVC in Android (For details, refer to the previous article "Introduction to MVC mode in Android"). activity or Fragment acts as the Controller. Next we will detail the specific steps:

1. in Android applications, users generally interact with each other through views, such as clicking a button to call an interface to request data or performing a drop-down refresh on ListView, therefore, we often use views to tell the Controller to request data. Of course, there are exceptions. For example, when loading an Activity, we need to request data first, in this case, you do not need to trigger the request through View in step 1.

2. this step is relatively simple, that is, sending http requests to the server. In Android applications, data is usually requested through http, and necessary parameters are included in the request process. In order to prevent ANR, this operation package or subsequent steps 3, 4, and 5 should be implemented in the Child thread.

3. The server returns data to the client, which is usually a lightweight and easy-to-transmit JSON format. Of course, XML format is also used.

4. After the client obtains the returned results in JSON format, we cannot directly use them. We need to use a Parser to parse the JSON data into the data model we need to obtain Models. For special data or product requirements, You Can cache the data. When the network is abnormal or you do not want to request data online next time, we can use the data directly from the Cache.

5. after the data is parsed into Models, the Controller can use it to update the view. After the data is parsed, a mechanism is usually set up to notify the Controller. There are many methods, for example, Android message mechanism.

6. The last step is that the Controller uses the data in Models to update the View. Note that the View must be executed in the UI thread.

This is probably the logic of the data request framework. The requested data will inevitably contain image data, so how to download and cache images is a big problem. Here we will summarize the overall data request framework, in the future, we will summarize the image download, cache, and loading frameworks.



For adding a service in the Android framework

Refer to the examples in framework/base/services/java/com/android/server. Generally, a server is composed of xxxxService. java xxxxManager. java IxxxxManager. aidl. Manager and aidl are in frameworks/base/core/java/android /. If you want to run the system at startup, you need to add the code to start your Service in SystemServer. java.

How to build a framework for the android C/S application system?

To put it simply, you can easily understand:
1. Physical System Architecture
Android client ------ wireless route ------- (web server, database)
2. Technology Selection
(Android client) ---> Web Server (tomcat) ---> database (mysql, etc)
After the client successfully requests data, it returns data (network communication can be performed through Web Service or HTTP)
(Android client) <--- Web Server (tomcat) <--> database (mysql, etc)
3. Create a client project WirelessClient (installed on the mobile phone at that time) and a server project WirelessServer (installed on the web server pc ). The two projects are developed separately. The mobile phone end uses the android API and the server end uses Java for development.

Does this help you simply understand it?

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.