Offline Android Store and Forward
Last week I was working on an android client that can send MI data over a rest api. We were particulary interested in knowing when the device has limited or no connectivity. This presented an interesting problem: how to send data to a rest api when you don’t have connectivity.
This is a fairly common problem, usually solved with a local store and forward service that batches up the requests until the connection is resumed whereupon the data can be sent to the server.
I rather naively assumed that there would be a standard android library that I could make use of, but was suprised to find this area lacking. There were plenty of large scale messaging systems that supported queuing using specialist client and server, but nothing to call simple rest web services once connection is re-established.
##The Solution We decided to create a simple open source android library to cater for this situation. We called it reyna (Icelandic for try) and the source is available on github.
The first challenge is the slightly crazy way that libraries are defined in android. You cannot ship them as .jars, instead you have to add the source to your project and perform the usual eclipse black magic to get it to build. Thankfully git submodules came to our rescue and allowed us to manage combining of the two repositories without too many hoops to jump through.
Next we needed to store the calls when the device had no connection. This was a fairly trivial case of using a sqlite database, and setting up the correct intents so that we resume sending the calls when the connection was resumed.
The api currently only supports POSTing to https web services, as that was all we needed. Additionally you can store headers to send along with the call; useful for any authentication tokens required.
I’m pretty pleased with the result, the code felt clean and simple, and as usual please go ahead and improve it.