Reputation: 22018
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
.
What's better to use as the value? The JSON
String, then deserialize it again with Gson
or to store the POJO
s? I know deserializing takes a little cpu time, but perhaps there's a downside to storing the POJO
s in my cache?
Say I have an Activity that diplays the content of a POJO
called 'Product' (that I got from the server as JSON
). On orientation change, can I just forget about keeping the Product POJO
(via savedInstanceState
or whatever) because the network call to retrieve it again will most probably be 'free' anyway (because it's still in the cache)?
Would it be better to have separate caches for different types of Objects or just use one big cache?
Any advise on how to determine a good size for the cache (in MiB
or just number of Entries)
Upvotes: 2
Views: 1668
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