fweigl
fweigl

Reputation: 22018

Caching Json / Rest network responses

For my Android app, I want to keep the network traffic as low as possible (of course). I'm aware of HttpResponseCache but it only works on API >= 13 so it's not an option for me.

I thought of of using using the LruCache, use the REST Url as the key (given theres no POST data). When I get a response from the server (JSON) I instantly create POJOs from it with Gson.

Upvotes: 2

Views: 1668

Answers (1)

Eric Levine
Eric Levine

Reputation: 13564

A good place to start is Virgil Dobjanschi's Google I/O 2010 talk on RESTful patterns for Android: http://www.google.com/events/io/2010/sessions/developing-RESTful-android-apps.html

In a nutshell, he advocates for using SQLite to keep track of the state of your HTTP requests and caching data to minimize requests. I found a sample implementation here, but you may want to search around for more resources on the patterns outlined in that talk.

Since originally answering this, several good open source libraries for Android that will handle caching HTTP requests have come into existence. OkHttp and Volley are two of solid options.

Upvotes: 2

Related Questions