Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android and a RESTful service

I am trying to implement an app to utilize a RESTful web service. I've studied several different examples and have coded a good working app that does successful pulls from the REST service. But now I need some direction. Here's some basic background (very simplified)...

  • Assume a basic REST service that has "GetReferrers" and "AddReferrer" methods.
  • From the Activity, I call a managedQuery to get a Cursor back from the ContentProvider for my ListView.
  • The ContentProvider returns back any local data, and calls an async "GetReferrers" to get the latest server list of referrers.
  • I have a custom ResponseHandler to parse the JSON and insert it into the ContentProvider.
  • As of now, I am deleting all records in the local ContentProvider upon a successful "get" from the server, then inserting the new/updated list to the database.

Two questions I have are...

  1. When I do a new GET, would it be common practice to delete all the existing records in the local ContentProvider and insert the new list? Or would it be better (albeit more time consuming) to do checks for new/changed/deleted records to only add/change/delete, respectively?

  2. I really don't have any idea where to start on doing an "add" from the client to utilize the "AddReferrer" REST method. I know how to add a new item locally to the ContentProvider using ContentValues with a getContentResolver().insert(). But, where would I put the code to push that up to the server? Or would it be more common practice to skip adding to the local ContentProvider, push it up to the server, then let the GET pull it back to the local ContentProvider?

Hope that all makes sense. I'd appreciate any direction you can give. If anybody knows of a good two-way example like this, please share. All examples of REST client operations I've found so far are just doing "gets" from the server and not pushes back up.

like image 217
robertmiles3 Avatar asked Nov 14 '22 22:11

robertmiles3


1 Answers

  1. When I had to do something like this once I could implement a REST method like getReferrersSince(timestamp), which gave me only the changes since my last sync. But if the service is not under your control I would also suggest the first option. Checking every entry for changes probably takes as long as deleting and re-inserting them all, but it's much more likely to be inconsistent.
    btw: Daniele Teti is right. So it was actually something like
    GET http://.../referrers?since=[timestamp]

  2. Just to make sure: Did you watch Virgil Dobjanschi? He suggests inserting the items locally first and marking them as "pending" in an extra database column (eg. SYNC_STATUS). Then have a service that uploads all pending items via REST in the background and marks them as "synced".

like image 172
jederik Avatar answered Dec 17 '22 11:12

jederik