I'm a bit confused about how to cache data in Android. I've seen many people implementing their own cache (eg, in droidfu project), but Android seems to have its own caching system with ResponseCache.
Is there any reason for not using Android cache?
What's the standard way to cache URLConnection response (text, data, json...), and where can I found examples?
Thanks
You have no control over the size of the cache or when objects get cleared (to my knowledge)
You cannot set the cache to the SD card
For small web-service requests the ResponseCache will suffice, and is the standard way of caching the raw response. It is only really when dealing with larger objects that you will need your own cache.
Alternatively you could
Serialize the data and save it to a local file
For basic data you could save it in SharedPreferences if it is long-lived
Related
I have an app where I take data from API (some points on map with properties like descriptions, lan, lat, and list of photos) because of offline mode. I am not sure if I should use sharedPreferences or some okHttp cache (or some ORM database). SharedPref is good for small values, not for list of objects. Do you have suggestions/best practices?
Thanks
Store your data in db with image URIs. Store Images in memory cache and retrieve them from their URI. Retrofit doesn't comes with support for image loading from network by itself. If you don't want to go into depth of all this, you can use Glide or Picasso.
Picasso saves full image and can be resized at the time of loading. Glide caches images after resizing. See what fits your case.
Storing and retreiving images directly from database will require too much processing and slow down loading of images especially if you need large images. For more information read check out developers note on Caching Bitmaps and Display Bitmaps Efficiently.
I would suggest you to use response cache if you require the response only to display. Retrofit provides a nice and handy response caching method, you can make use of Interceptors to cache response. hope you are using retrofit latest version 2.1.0. check out this link to get more.
If you wanted to perform some operations like marking favourites etc. you can go for database.
I have app that suits on Facebook, VK apps. I need to make a cache system to have offline access to some pages and improve a time of the data loading. I never make cache system earlier... So I very want to listen advices from people who built so systems. For example, how to save data better in database or in files. If it's database then where the best way to storage it in android app cache folder or use a simple built-in sqlite database... I will very glad to all answers by my theme.
You can cache items using the LruCache available in Android. LruCache
As you are trying to save Json objects, then the effective way is to retrieve the Json objects using a network library such as Volley and save the Json String in a SharedPreferences file. And you can parse it to load into a View whenever you want! And you can cache the Images using the NetworkImageView of Volley with LruCache! This helps you to cache and populate images and texts trouble free ! The perfect tutorial is already available here
Also, here is the code to my implementation as well!
I'm currently developing an android app in which I'm dealing with php files that returns json, the json then gets parsed and is used to fill up an arraylist of different objects.
What I'm trying to do is, I want to cache these objects in case of no internet so I can reload them and make the application useable offline.
Currently I have 2 solutions for this:
A) Make an SQLite database and creare tables with the same structure as the objects I'm trying to cache and then reload them on startup
B) Save the JSON strings inside the shared prefs and parse them on startup.
I didn't really find any best practices or tutorials when I came across data caching so I'm lost now and I have no idea what to do. So if you guys can please help out I'd be thankful.
You might find the following talk interesting:
https://www.youtube.com/watch?v=xHXn3Kg2IQE
As you can see, a lot of scaffolding goes into the ContentProvider approach. For a very simple app, it can be acceptable to just cache some JSON in a SharedPreferences. However, as your app becomes more complex, the advantages of the ContentProvider will be worth it.
You can use files instead of sqlite and shared preferences.
Shared prefrences is using for small data.
Sqlite is using when you need to make queries on stored data.
So I think it's better to cache in files, or if you are using something like retrofit or any network lib you can check if they support caching and use it.
Definitely not option B. Shares preferences are not suitable for storing large amounts of data. It's XML so cannot be queried like Sqlite. The time to retrieve a single item will increase linearly with the amount of data stored. Secondly JSON will have special character these will need to be escaped which means the storage size will increase even more.
There is however an option C. Using cache files. This approach and other store options available to you are described in the google developer guide and this is essential reading.
So in summary: you options are to parse the JSON and store it in sqlite or to save the json as a file in the cache directory.
We load lots of data as JSON objects to some arrays in our application and this is handled while online. but now we would like to move to some kind of caching, how can we implement off line mode with Android?, is there an external storage I can write to?, there could be thousands of records.
You could use the LruCache for caching objects. Here's an example on how to cache bitmaps for example.
I have an Android application that would retrieve data (images+text) from a php remote server and display them in a GridView.
I am doing the operation in the background using Loaders. I have separate connections for images and texts since retrieving images would take longer and I want to display the texts immediately. The texts are encoded with Json on the server after being retrieved from MySQL. On the app, I am parsing the Json Objects and displaying the texts as I need.
The problem is with images. I am not sure if encoding the images with Json would be a good idea. Also the images are saved as blob in the database, in order to encode them with Json I need to use base64_encode() before which is not efficient. I have seen many posts about this, but it’s always a simple example when you have to get one image. In my case I’ll be retrieving up to 30 small-size images.
My question is, I can proceed with what I just presented, but it seems that there should be a better way to do this. What do you think about this? Am I going the wrong way?
Also I was thinking if I can display each image separately in the gridview once it is ready (not waiting for all the images to be ready) just like in the “Google Play App”’s GridView. What approach can I take to achieve this?
Thanks in advance folks!
Best approach in my eyes would be to download the image files as normal image files via a HTTP get request. Make sure it is threaded of course, and have a thread pool that you can queue up requests into, and have 2-3 threads go through and download.
In terms of saving them, I would personally move away from saving to blob in a database, and opt to save them to the persisted storage in your application's private directory. Saving the image files with their filename as their id in the database you have created will be much quicker for loading them back in.
You can also hold a reference to the ImageView, and have it display a place-holder initially, with a successful HTTP request replacing the bitmap of the ImageView with the one you have just downloaded/read in from storage.
You can also do some image caching within the HTTP request you make.
ImageView myImageView = findViewById(R.id.testImage);
URL url = new URL("http://www.website.com/image.jpg");
URLConnection connection = url.openConnection();
connection.setUseCaches(true);
Object response = connection.getContent();
if (response instanceof Bitmap) {
Bitmap bitmap = (Bitmap)response;
myImageView.setBitmap(bitmap);
}
It also may be helpful to lookup the uses of the LRUCache, which performs a lot of caching functionality for you.
Check out this link at the Android Developer site for a good in depth guide to image caching
Edit:
You can use the advice in Robert Rowntree's answer to load bitmaps more efficiently to cut down on your memory use as well. The link provided details loading of bitmaps using less memory, something that would work well if you are creating thumbnails from larger images downloaded over the web and saved off to local storage.
IMO - there are 2 issues , moving the images across the network to the client and getting them loaded.
Assuming that you are using http as the protocol, you should have a multithreaded solution for http as is available in apache httpclient package. That will get the pictures to the phone fast.
Then , you have to present the pics by getting them into memory and a cache. Here you can consider what 'gallery3D' app does with its grid and bitmaps but its pretty complicated to read thru that code.
check out - http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
check out code samples for loading thumbs from bitmaps.