Atma
Atma

Reputation: 29767

Best way to load external data in android

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

Currently, this is what I do:

  1. Create RESTful web service that returns a JSON Array of objects (on a server)
  2. In android invoke HTTPGet and consume service
  3. Parse through response JSON
  4. Use GSON to parse response straight into an array of objects
  5. Use the array of objects as needed

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

Upvotes: 5

Views: 5028

Answers (1)

Rob
Rob

Reputation: 4753

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

  1. Create RESTful web service that returns JSON (or XML)
  2. Your activity (via AsyncTask or Loader) requests resources from a ContentProvider
  3. ContentProvider returns a Cursor containing the data it has cached in a local Sqlite Db
  4. ContentProvider asks a Service (or SyncService) to collect fresh data from the web service
  5. Your activity makes use of Cursor to display data in UI
  6. Service invokes HttpGet
  7. Services parses response
  8. Service pushes new data into ContentProvider (which in turn updates Sqlite db)
  9. ContentProvider calls notifyChange to inform app there is new data for Cursor returned in step 3
  10. Your activity re-requests an updated Cursor from ContentProvider, and then updates UI with fresh data in Cursor

Upvotes: 16

Related Questions