Best way to load external data in android
What is the best way开发者_C百科 to load external data in android?
Currently, this is what I do:
- Create RESTful web service that returns a JSON Array of objects (on a server)
- In android invoke HTTPGet and consume service
- Parse through response JSON
- Use GSON to parse response straight into an array of objects
- Use the array of objects as needed
Is this the optimal approach in terms of the Android documentation?
According to the Google I/O Creating REST apps presentation, you should do something like:
- 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
精彩评论