Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 529
Atma Avatar asked Oct 06 '11 23:10

Atma


People also ask

How can you access data from external storage in Android?

The data files saved over external storage devices are publicly accessible on shared external storage using USB mass storage transfer. Data files stored over external storage using a FileOutputStream object and can be read using a FileInputStream object.


1 Answers

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
like image 200
Rob Avatar answered Sep 28 '22 08:09

Rob