How to use Volley Cache — Android Studio

Ankit Suda
Android Grid
Published in
2 min readDec 1, 2017

Hello folks, in this tutorial you’ll learn how to use Android Volley Cache.

Before we begin: I’m using Ubuntu Linux and also using XAMPP as a server, so make sure if you have XAMPP installed in your system or just put the JSON data to your website. And I am using Android Studio 3.1 Canary 4 for this tutorial.

Some of the code is same as this tutorial, so you can read this tutorial to put JSON data in RecyclerView.

Source Code is at bottom of this post.

How the JSON will look like:

The JSON’s root is “[“ and all data is wrapped in “{“.

“title” = String  
“rating” = int(Number)
“releaseYear” = int

Volley Cache:

Code for Volley cache is little bit complicated but it’s easy to implement-

If you have a JSONObject or any other request just replace JSONArray to your request type.

Let’s Code:

Step 1) Create a new Android Studio project and add these dependencies to app level build.gradle

Step 2) Open activity_main.xml and add a RecylerView attribute

Step 4) Create a new Java Class and name it Movie and modify it with below code

Step 5) Create a new Java Class for adapter and name it MovieAdapter and modify it with below code

Step 6) You might get a error R.layout.single_item to fix this create a new layout and name it as single_item and add below code in it

Step 7) Add below variables in MainActivity.java

Step 8) Declare the variables

Step 9) If you’re using put your JSON file to /htdocs/Volley/ . If you’re using a real website and have control to FTP put it to your website.

Step 10) If you using a real website use you own domain, if you’r at XAMPP you need server IP instead of localhost. To get the IP Address: If your using Windows/Mac use ipconfig instead of this if you’r using Linux like me, use ifconfig to get the IP Address.

Step 11) Now you’ve got you Server Address, create a new String variable to MainActivity.java

Step 12) Now we’ll fetch the data, create a new method in MainActivity.javaand use below code

Step 12) Execute the method in onCreate()

Step 14) Now yourMainActivity.java will look something like this

Step 15) Finally add Internet permission in manifest

Step 16) Build and Test the app in an Emulator or use a real device if you’re using a real website. Clap and Comment if you got any problem in this code.

Source Code

https://app.box.com/s/ali7jbms3oqh4zvc3kn1nzmbtljac22e

--

--

Ankit Suda
Android Grid

Ankit is self-taught Android App Programmer. He provides Android App Development Tutorials to Android Grid.