加载在Android的外部数据的最佳方式加载、方式、数据、Android

2023-09-05 09:47:13 作者:兮兮

什么是加载外部数据在Android的最佳方法是什么?

What is the best way to load external data in android?

目前,这是我做的:

创建REST风格的Web服务,它返回一个对象JSON数组(在服务器上) 在Android的调用HTTPGET和消费服务 通过解析响应JSON 使用GSON解析响应直入对象的数组 根据需要使用对象的数组

这是在Android文档?

Is this the optimal approach in terms of the Android documentation?

推荐答案

按照谷歌I / o创建REST应用 presentation,你应该做的是这样的:

According to the Google I/O Creating REST apps presentation, you should do something like:

创建返回JSON(或XML)RESTful Web服务 您的行为(通过AsyncTask的或装载机)请求的资源来自ContentProvider的 的ContentProvider返回包含它在一个本地SQLite分贝缓存数据的游标 ContentProvider的询问服务(或SyncService)从Web服务收集新的数据 您的活动利用光标在UI显示数据 在服务调用HTTPGET 在服务解析响应 服务推新数据的ContentProvider(这反过来又更新sqlite的分贝) 的ContentProvider要求有NotifyChange通知应用程序没有在第3步返回游标新数据 您的活动重新申请一个更新光标从ContentProvider的,然后更新UI新鲜的数据游标 Create RESTful web service that returns JSON (or XML) Your activity (via AsyncTask or Loader) requests resources from a ContentProvider ContentProvider returns a Cursor containing the data it has cached in a local Sqlite Db ContentProvider asks a Service (or SyncService) to collect fresh data from the web service Your activity makes use of Cursor to display data in UI Service invokes HttpGet Services parses response Service pushes new data into ContentProvider (which in turn updates Sqlite db) ContentProvider calls notifyChange to inform app there is new data for Cursor returned in step 3 Your activity re-requests an updated Cursor from ContentProvider, and then updates UI with fresh data in Cursor