Android JSON Parsing Using Volley From URL Example Tutorial

Welcome to Android JSON Parsing Using Volley From URL Example Tutorial.

We will learn how to parse JSON from URL or Server Using Volley Library.

You will learn how to fetch/retrieve data from MySQL database in android using json, volley and PHP.

When we want to interact with the remote server, we need to send and fetch data over internet.

For example, when making registration and login module, we need to send username and password to remote server when user completes sign up task.

When user wants to login again, we need to fetch those username and password from the remote server.

In this process of sending and fetching data, we have to make some bridge between android device and remote server.

This tutorial uses Volley as a bridge.

Volley will return our json text in the string format.

Information About Volley

Volley is a networking library developed by google.

It is an HTTP library that makes networking for android apps easier and faster.

We can also use AsyncTask class for networking purpose which is in-built class for android system.

Main drawback of using AsynTask is it’s inaccuracy. Also little time consuming problem is there for this class.

Steps To Make Example

Follow all the below steps to make a sample project in android studio.

We will fetch JSON data using volley and parse this data using JSONArray and JSONObject class.

Step 1. Dependency part

All the classes of the Volley library are not included in the core android structure.

They are available on GitHub but not in local android library.

So we need to fetch the volley classes in our project using dependency concept.

Add the below line in build.gradle(Module:app) file

Above line will enable us to use volley library in our project.

Step 2. Model For Players

Now let us create a model class. This class mainly includes getter and setter methods for the various data types.

For example, we have id, name, county and city of the players.

So we will have separate getter and setter methods for all four data types.

Make a new JAVA class named PlayerModel.java 

Copy the below code in PlayerModel.java

  • As you can see that we have required methods for all four data types in above class.
  • We will use getter methods to fetch the data from the JSON response.
  • Setter methods will help us to set the data in appropriate UI widget.

Step 3. Main Files

Ok, now last thing is to change activity_main.xml and MainActivity.java files.

Write down below code structure in activity_main.xml

  • I have defined two textviews in this file.
  • First one is just saying that compiler have fetched the below data from URL.
  • Second one will hold all the data from json response.

Now add the following coding lines into MainActivity.java file

Understandings Of Main Logic

Main logic and code is written in the Main Activity.

First of all, read the below lines

  • First line is declaring one string variable named “URLstring” . This string includes the URL from which we will fetch JSON response.
  • Second one is making an object of the textview where we will set the data.

requestJSON() method

  • in onCreate() method, I have written a requestJSON() method, which will parse the json.

Following is the coding layout for requestJSON() method

Consider the below code

  • Compiler will create an object of the StringRequest class.
  • First parameter will decide whether volley will execute GET request or POST request. Our URL do not need any URL parameter so we will execute GET request.
  • Second parameter includes the string variable. This string variable represents the URL which will give us JSON response.
  • When the volley get any response from the URL, compiler will run onResponse() method.
  • We will receive our response via the parameter of onResponse() method.

Look at the below few starting lines of onResponse() method.

  • Compiler will implement try statement so that if any execution is there, we can avoid force close (app crashing) of our app.

JSON response is looking like below

  • Under try statement, compiler will first get the JSON Object.
  • then it will check the status.
  • If the status is true, then it will create an Arraylist with the objects of the PlayerModel class.
  • Then it will create a JSONArray named “data”. You can see the JSON array named “data” in above JSON response.

Now consider following code snippet

  • Compiler will execute one for loop.
  • Here, it will create an object of PlayerModel class and an object of JSONObject class.
  • The it will get the id, name, country and city of the players and will set them using setter methods.
  • After this, compiler will add the object to the arraylist.
  • After populating the arraylist with the data of the JSON response, compiler will execute below loop

We will write text in the textview using the above loop. It will write all the information about each player during every iteration of the loop.

Final Looks of JSON Volley

Download Source Code For Android JSON Parsing Using Volley

[sociallocker]Download Source Code For Json_Parsing_URL_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...

Leave a Reply

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