Hello friends, In this tutorial I’ll show you that how to get data from Rest API and store them in cache, so whenever you left current activity and come back to same activity then you don’t need to fetch data again from API. This will reduce API load and speedup your app. The Idea behind this cache memory is that we will get json data from API and save that json data in json file in mobile’s SD Card or cache memory according to our need.
You all use Facebook, Instagram app right? And you noticed that when you open facebook app then data is already loaded which you seen previously and then data get updated automatically, and you don’t need to wait to load data and no blank screen.This is good idea to display old data instead of loading screen or progress bar, which is too annoying. Hope you understand why we need this.
We can also do this with SQLite database, but this process is more faster than SQLite, its fater because we get data from API in json format, so will will write the same data in json file. Means we will not parse each and every JsonObject and JsonArray. In case of SQLite we need to parse JsonObjects and JsonArray and all the values exist in API response. And when we display that data into our RecyclerView or any other UI component then we need to get data from SQLIte DB and convert then into model class -> add that model class into ArrayList -> and finally set that ArrayList to adapter.
So in json files we will get json response from API and write that json data As it is into json file. No need to parse json data. And when we use that json data we simple get json from json file and process them same as we process data from API and display in RecyclerView.
In this example I’m using Retrofit2, the same example will be applied to Retrofit1 and any other json parsing technique like Gson, Volley and HttpUrlConnection.
public static boolean isConnectionAvailable(Context context) {ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);if (connectivityManager != null) {NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();if (netInfo != null && netInfo.isConnected()&& netInfo.isConnectedOrConnecting()&& netInfo.isAvailable()) {return true;}}return false;}public static void l(String TAG, String msg) {Log.d(TAG, msg);}public static void l(String TAG, Throwable e) {Log.e(TAG, "",e);}
Quick Links
Legal Stuff
Social Media