Android JSON Parsing Using Volley And Display With RecyclerView

Android JSON Parsing Using Volley And Display With RecyclerView is today’s tutorial.

First of all, we will parse the JSON from URL using Volley.

Then we will populate a recyclerview using the data fetched from the JSON.

Generally, we need to make http calls to use the data on the remote server but volley simplifies this process.

Volley will reduce the number of Coding lines to make GET or POST calls to the web service.

We will fetch the information like name, country, city and image URL from the JSON.

Along with recyclerview, we will also use cardview to make it looking more attractive.

RecyclerView From Volley

Step 1. Internet Permission In Manifest

Internet is the basic requirement to call JSON from the remote server.

Add the below line in the AndroidManifest.xml file

  • Here, we do not need to code for runtime permissions because internet is the normal or basic permission.
  • Normal permission means that it is lesser harming to the user privacy.

Step 2. Some Dependencies

It is time write some dependencies in the build.gradle(Module: app) file as per the below

  • I have added these lines for four different purposes.
  • First one is for volley library. It will enable us to use volley classes with any other integrations.
  • Second line is for picasso library which helps us to fetch the image from URL seamlessly.
  • Third line will allow us to use the recyclerview.
  • And finally, fourth line will give us the required classes for the usage of cardview.

Step 3. XML File For RecyclerView

Let us create an XML file under res->layout directory.

Give it a name rv_one.xml and add the following code in it

  • This file will provide the view structure to every row of the recyclerview.
  • I have taken one imaheview and three textviews in this file.
  • All these widgets are under the cardview that will create card layout to every row item.

Step 4. Data Model

Let us create a new class and give it a name

Code structure for is as the following

Step 5. Required Adapter

Adapter class will help us to populate the recyclerview.

Make a new JAVA class and give it a name

Code block for is as the following block

Consider the below lines

  • First compiler will define the objects of LayoutInflater and arraylist.
  • Constructor will give us context and arraylist via it’s parameters.

Look at the following code block

  • First line is loading the Image from the URL.
  • Compiler will use Picasso library here. It will get the url using getter method.
  • Compiler will write the name, country and city using related getter methods from the objects of the arraylist.

Step 6. Main Modifications

Add the following lines in activity_main.xml file

It includes one textview and recyclerview.

Now write down the following lines in file

Broad View Of Main Class

Read the below lines

  • First line is defining the URL of the Web service.
  • We will parse JSON using this URL via volley.
  • Second line is making the object of the progrss bar.
  • Third one is creating an arraylist with the objects of the DataModel class.
  • Fourth is making on object of RvAdapter and fifth is of RecyclerView class.
  • Inside onCreate() method, compiler is calling fetchingJSON() method.

Following is the code structure for fetchingJSON() method.

  • This method will parse the JSON and will populate the arraylist.

Json that it will parse is as the following

  • fetchingJSON() method will make an array (dataArray) using JSONArray class.
  • Then compiler will execute a for loop for the number of times equals to the number of items of the daraArray.
  • During every iteration, compiler will create one object of the DataModel class.
  • Then it will set the values using setter methods and finally, it will add the object into the arraylist (dataModelArrayList)
  • Then compiler will call the setupRecycler() method.

Code for setupRecycler() method is as the following

  • In this method, compiler will create the objects of the RvAdapter class.
  • It will use dataModelArrayList while making this object.

At last, this adapter is set to the recyclerview and it will create recyclerview from JSON data.

Download Source Code For Android JSON Parsing Using Volley And Display With RecyclerView

[sociallocker]Download Source Code For Json_Recycler_Volley[/sociallocker]

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

2 Responses

  1. THOMAS says:

    Great tutorial. Thanks for making it. Any chance of seeing the code for json_parsing.php?

    • Hardik Parsania says:

      Here are the coding lines for json_parsing.php

      < ?php //including the database connection file include_once("config.php"); $query="SELECT * FROM demo_table"; $result= mysqli_query($con, $query); //create an array $emparray = array(); if(mysqli_num_rows($result) > 0){
      while ($row = mysqli_fetch_assoc($result)) {
      $emparray[] = $row;
      echo json_encode(array( “status” => “true”,”message” => “Data fetched successfully!”,”data” => $emparray) );

Leave a Reply

Your email address will not be published. Required fields are marked *