How to implement offline mode on android - android

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.

Related

The best way to build cache

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!

Caching in Android

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.

Android Storing RSS feed parsed data in a database for offline use

I am parsing a Rss feed using sax parser in android. I am able to display the data online when net is connected but i also want it to work for offline use and update it when it gets connected again to internet / wifi. I have no clue how to go about it.
What should be my best approach now ? should i construct Sql database ? considering i have images as well.
Or there is any other simpler way. I would prefer simpler way.
I need some further suggestion on the Sql database approach here, First : My rss feeds gives image url links which i diplay using bitmap and insutstream at runtime but now for offline purpose i need to save complete images like whatsapp does right ? is this right ? if yes how to save complete images in database ? And last i want to save the complete database on sd card not in internal memory , storing data on sd card will work fine or it will create problem ? because whatsapp stores quite a data in internal memory !! if storing on sd card is not a problem how do i store complete data on sd card ?
This depends on how long you want the data to persist. Ask yourself:
Should this data be available to the users after rebooting the phone, or after force closing the app? Should it be available regardless of the last time I had connectivity, as up to date as possible given that?
In that case, then yes - you should use a database. Android has a number of built in helper classes for sqlite databases.
http://developer.android.com/training/basics/data-storage/databases.html
Which should get you started.
The images are pretty straight forward as you'll just stash a reference to the image(s) in the db. You would of course write these images to disk as well (on the sd card or some other place...) See:
Save bitmap to location
Your other options, afaik are:
1) SharedPreferences (not really suited to this).
2) Serializing your data and writing out/reading in from some file.
If you're still looking for more information on Database concepts and Android, here is a very good tutorial on the topic:
http://www.vogella.com/articles/AndroidSQLite/article.html
You can use droidQuery to download the RSS Feed and cache it. Working off of this gist, which expects you to use the android-rss library, you can add the following cache flags to your AjaxOptions object:
.cache(true).cacheTimeout(AjaxCache.TIMEOUT_NEVER)
This will make it so the response is cached until you explicitly call:
AjaxCache.sharedCache().clearCache();
Which you can do after the network is connected (for help on this, check out NetWatcher).
Note that using this cacheing mechanism allows a very simple solution that will only store as long as the app process is live. If you want to save across sessions - so that if the user opens the app later and is not online, you will want to use something more complex and long-lasting, such as SharedPreferences or SQLite. A good list of options can be found here.

Standard method for caching data in Android?

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

Caching downloaded JSON data to SQLite database - is it a good idea?

In my app I have to download JSON data from numerous web services. The data classes I use are fairly complex ones (lots of properties, quite deep inheritance tree, etc.).
I intend to do caching, using a single db table, where I'd store the downloaded JSON data in a VARCHAR column (along with other meta-data containing columns). JSON serialization is being done with the Gson library.
It seems quite convenient to just dump the instances into JSON, and parse them again later when I need them. No need to create custom tables for every class, or write loads of custom serialization code. Also, I can do queries on the cache table this way.
The question: Is this approach an anti-pattern by any means?
There is absolutely nothing wrong with this approach; however, I am going to recommend that you instead use the built in caching storage. See the section called "Saving cache files" in Data Storage for more details.This way you don't hog any precious space if your JSON objects are large in the event of a low memory situation.

Categories

Resources