Learn Pain Less

HomeOur TeamContact
Android
Fetch data from API and store in cache
Pawneshwer Gupta
Pawneshwer Gupta
August 01, 2017
2 min

Table Of Contents

01
Fetch data from API and store in cache
02
Why we need to do this?
03
Why I'm not using SQLite DB or any other DB?
04
How json files are after than SQLite DB?
05
Complete Example of storing data into cache
Fetch data from API and store in cache

Fetch data from API and store in cache

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.

Why we need to do this?

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.

Why I’m not using SQLite DB or any other DB?

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.

How json files are after than SQLite DB?

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.

Complete Example of storing data into cache

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.

CacheManager.java

CacheUtils.java (to save name of json file)

MainActivity.java

M.java file

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);
}

ItemLatest.java

APIService.java

YoutubeApi.java

Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.

Tags

Retrofitcacheandroid

Share


Pawneshwer Gupta

Pawneshwer Gupta

Software Developer

Pawneshwer Gupta works as a software engineer who is enthusiastic in creating efficient and innovative software solutions.

Expertise

Python
Flutter
Laravel
NodeJS

Social Media

Related Posts

How to use Text To Speech inside RecyclerView in Android
How to use Text To Speech inside RecyclerView in Android
July 27, 2020
1 min
Learn Pain Less  © 2024, All Rights Reserved.
Crafted with by Prolong Services

Quick Links

Advertise with usAbout UsContact Us

Social Media