5

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.

JJD
  • 50,076
  • 60
  • 203
  • 339
robertmiles3
  • 899
  • 10
  • 20
  • for #2, i would first push the data to the REST service, and upon a successful response, insert locally. – james May 24 '11 at 21:03
  • I think this is what I'm leaning toward for #2. – robertmiles3 May 24 '11 at 23:43
  • @robertmiles3 I hope you successfully went through this. Maybe you can help me with this [similar setup](http://stackoverflow.com/questions/11906172/synchronize-android-client-and-rest-server). – JJD Aug 10 '12 at 17:59

3 Answers3

2
  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".

John Conde
  • 217,595
  • 99
  • 455
  • 496
jederik
  • 352
  • 2
  • 7
  • I'm not sure I can go with Virgil's method. If I insert a new "referrer" and it hasn't synced up yet, then I try to use that referrer with a different post method it would fail. The referrer would have to be pushed up successfully first before I could use it elsewhere. – robertmiles3 May 24 '11 at 23:40
  • True. You'd have to be careful in which order you sync. But you can already referre to the local entry locally. That way you don't have to wait for the server to respond to the user which would be a lack of responsiveness. But I admit that this is not trivial and you should better think hard about it;) – jederik May 25 '11 at 00:38
0

This is just my opinion, but for question #1, I would recommend just deleting the current records in the DB and then re-insert them upon a successful web call. With the later suggestion, you would have to create a function to loop through all of the current values in the DB, see if they match the new collection (I assume you would have some sort of primary key to make the connection) and then update / remove / add values to your DB which could possibly be numerous calls to your DB unless you create some generic update statement where you build the 'SET / WHERE' clause dynamically as a string and then run the query once.

Either way, from an efficiency standpoint, running just a delete and insert query versus a complex matching function plus multiple update / add / delete queries seems the best way to go, again in my opinion.

For question #2, you have to build the XML in your code and then post it like these examples:

Android Post Example

Another Example

Or, you could just pass your values via the URL string and then let the server extract those values.

For example:

http://www.example.com/MyWebService.asmx/AddReferrer?variable1=foo&variable2=bar&variable1=foo2&variable2=bar2

In that example I'm assuming the server is a .net server. In .net I can basically create an array of all of the values for variable1 and then loop through them and add them server side to a DB for example.

[WebMethod]
public string AddReferrer(string[] variable1, string[] variable2)
{

Hope this helped!

AngeloS
  • 5,536
  • 7
  • 40
  • 58
  • Thanks. I think we agree on #1. It's much faster and easier to just purge the existing records and re-insert. As for #2, the REST API I'm talking with requires a JSON object in the request. That's no problem, just an FYI that it's not .NET. The more I think about it, the more I'm wondering if I should just make some sort of async call to the REST post, then make the ContentProvider refresh its "get" to pull down the new record so that local storage gets updated. I'm open to any more thoughts as well. – robertmiles3 May 24 '11 at 00:24
  • Is there any way to modify the `AddRefferer` to return a response similar to the `GetRefferer`? Or, are these web methods a third-party web service that you can not modify?.. because if you can get the `AddReferrer` to send back a response that is identical to the "get" method, it would eliminate the need to have two async web calls, which on mobile devices would be preferable as you may have many users who may have poor network connections. – AngeloS May 24 '11 at 14:23
  • Also, here is a link to post json: [android post json](http://localtone.blogspot.com/2009/07/post-json-using-android-and-httpclient.html) – AngeloS May 24 '11 at 14:27
  • Can't edit it. It's a third-party REST API. In the meantime, I've got a working "post" for adding the referrer. Thanks! – robertmiles3 May 24 '11 at 18:44
-1

In the methods names you should not have verbs. In a RESTful system the "action" is implicit with the http request verb. AddReferrer should be called "referrers" and should be invoked with PUT http method. Check the following article http://www.infoq.com/articles/rest-introduction

Daniele Teti
  • 1,764
  • 14
  • 20
  • Agreed, and they do not have verbs in their methods. The method names above are not the real ones and were purely for the explanation of what I'm after. You've missed the point completely. – robertmiles3 May 24 '11 at 10:59