My little software store

Random thoughts about my experience as moonlight software developer.

PostmanLib - Android and Rest Web Services

Edit 2013-03-10: I've performed a substancial refactoring of postmanlib, and the details can be found in this new post

With the development of MyDearSanta app, I needed to fetch and put some data to a rest service developed using django python framework with a simple set of json based rest apis.

The development of the interaction between android and the rest web service was a bit messy because I was in hurry to release the app before Christmas (giving it's xmas related nature), but while developing it I thought it could have been a good start for my first android library. And so it was.

Yesterday I finally managed to write a first use case of my PostmanLib (rings twice) android library. I must confess it tooks nearly a couple of hours just to get a nice name, but I really like this one.

You can check the library HERE

It's barely inspired by Virgil Dobjanschi talk at google io 2010, and its relies on the common pattern is to make the one-shot background work inside intent services, let them update the model with the results of their work and then notify the interface when the model it's changed, so that it can refresh itself.

It does not provide a way to serialize your data into json nor how to store it into a content provider. There are a plenty of examples out there if you look for json, android, rest and other keyword like those and what's more, they really depends on the interface your server is offering.

What was missing was a common and clean way to interact with the service without using the "asynctask inside an activity" approach, which I don't really like because of the well known issues of configuration changes.

All you need to do with my lib is to implement the abstract methods of a Command class, such as the execute method that will be called inside the intentservice which is the method that performs the real work, and some method to serialize / deserialize the command inside an intent.
The library comes along with a singleton helper class that can be used in activities to fire the commands and get notified with their results.

The steps are:

  • the activity gets a reference to the helper
  • a command is sent to the intent service (serialized in an intent)
  • the intent service executes the command and updates the model (sqlite, shared prefs, content provider, whatever)
  • if the activity was registered as event listener, gets notified of the result and can check again the model (or notify the user about an error)
  • the activity can ask the helper about any in progress services (it can be useful in onresume stage to notify the user)

You will still have to implement the real interaction between Android and the web service

 It's far from be completed, but I am really enjoying how it is growing, and I hope it might be of some help for somebody.

If you are curious about that, just checkout it from github here Android rest service interaction