Android Volley Tutorial – Fetching JSON Data from URL

Hey guys, welcome to Simplified Coding. This post is about Android Volley Tutorial. And here we will do a straightforward thing. I have a URL that gives me some JSON data, and by using Volley I will get that data, and I will also parse it. So if you want to learn how do we fetch and parse JSON from a URL in android then let’s start this Android Volley Tutorial.

Note: This tutorial covers only sending an http get request to a URL using Volley if you want to learn about all the http requests you can watch this playlist. 

What is Android Volley?

Now if you are a newbie then you might be thinking that what the hell is Volley :P. So the answer for this question is “Volley is an HTTP Library  that makes networking for our apps easier and faster”.

Why Android Volley?

Volley simplify the networking task in android and with volley it is

  • Easy to use request management
  • Efficient network management
  • Easily customizable according to our need

Android Volley Tutorial

Now lets begin the main task, the android volley tutorial. So as I told you at starting I have a URL that contains some JSON data. So below you can see my URL.

You can use the same URL for your project. If you open the above URL, you will see the following. (Here I am using postman so that we can see pretty JSON structure but the browser will also show the same thing without formatting).

json data android volley tutorial

Now you can see we have some JSON data. At first, we have a JSON object, then inside that object, we have a key name heroes that contain an array of JSON objects. Each object in the array has two more keys name and imageurl. So our task here is to fetch this data and display it into a ListView.

But before moving ahead, if you think you should learn JSON first, then here is a seven minute quick video to learn everything about JSON.

Creating a new Android Studio Project

  • As always the first step is creating a new Project. So here I am creating a project named MarvelHeroes. Now name doesn’t matter at all you can create your project with any name you want. Just remember you have to select an Empty Activity while creating the project.
  • Once your project is loaded we will define internet permission on the manifest as we are going to perform a network operation and it requires internet permission.

Adding Internet Permission

  • Open the AndroidManifest.xml file and add internet permission as shown below.

Adding Volley Library

  • This is the important step 😛 (LOL). As we are going to use volley it is obvious that we have to add volley to our project. Don’t worry adding volley is quite easy.
  • Just open your app level build.gradle file. If you are confused what it is then see the screenshot below.

app level build gradle

  • Inside this file you need to add volley library inside dependencies block, as show below.

  • Now sync your project and you are done with adding volley library.

Data Model Class

  • To store the fetched data in objects we will create a simple model class. So create a class named Hero.java and write the following code.

  • The above class will be only used to hold the json data that we will fetch. That is why the class is having only a constructor to initialize the fields and getters to get the values.

Creating a ListView

  • As I told you already that we will display the fetched data in a ListView. And here we are going to create a custom ListView.
  • So first inside activity_main.xml create a ListView. In the below code you see we have a ProgressBar as well. This is because when the data is getting fetched we will show it so that user can get that something is loading.

  • Now we need a layout for our list items.

Custom Layout for List Items

  • Inside the layout directory (res->layout) create a new layout resource file named list_items.xml.

  • As we have two items (name and imageurl) in our json data, we created to TextViews here to display the fetched data here.

Creating Custom Adapter

  • As we are creating a custom ListView so we need to create a custom ArrayAdapter for our ListView.
  • For this create a new class named ListViewAdapter.java and write the following code.

  • Now the last part is fetching and parsing the json and displaying it to the ListView.

Fetching and Parsing JSON using Volley

  • Now come inside MainActivity.java and write the following code.

  • Now run the application.
android volley tutorial
Android Volley Tutorial – Fetching JSON
  • Bingo! It is working absolutely fine.

Android Volley Tutorial – Fetching JSON Source Code

  • Even after following each step carefully, you are having some troubles then here is my source code for you. Remember it is always your fault not the computers, and sometimes we think that we did the right thing, but it was our fault. So here is my tested source code for this Android Volley Tutorial.

So that’s all for this Android Volley Tutorial friends. If you have any confusions or queries feel free to leave your comments.

And if you found this post helpful, then please SHARE this with your friends and tell others about Simplified Coding. Thank You 🙂

39 thoughts on “Android Volley Tutorial – Fetching JSON Data from URL”

  1. cerrection:
    TextView textViewName = (TextView)listViewItem.findViewById(R.id.textViewName);
    TextView textViewImageUrl = (TextView)listViewItem.findViewById(R.id.textViewImageUrl);

    Reply
  2. hey am getting an error in my php scripts which is telling me this:

    br />
    Fatal error: Call to a member function prepare() on null in
    C:\xampp\htdocs\android1\include\DbOperations.php on line
    62

    can u help me plz?

    Reply
    • In this case make sure the SQL query is correct. To confirm the query is correct open your phpmyadmin and try executing the query you written on the code, and confirm it is working fine or not.

      Reply
  3. Hi Belal! Thanks for this tutorial. I would like to ask that, how do i able to display IF the data is empty in the database and it will show “Data is Empty” in a fragment or a dialog box ELSE it will straight display the data IF the database table are inserted a data. Thank you!

    Reply
  4. when i start the app then it will show recent search from json then i have to clear the cache of the app then it with show the latest search so please help me that it will refresh every time app with start.

    Reply
  5. Thank you belal. I am seeing this type of tutorial for some days but not find good but not i found this thanks.

    Also when i change my php code and add some more details then it will not show. then i have to clear data of app and then it will refresh so plz tell me about this problem that ho can i fix it.

    Reply
  6. I have no volly library dependency in my android studio 2.3.3, what to do?? As during adding dependency and syncing error occurred.
    I need a help sir.

    Reply
  7. What if we want to send array to php?

    For example
    //java
    params.put(“s1”,max[0]);
    params.put(“s1”,max[1]);
    ….
    params.put(“s1”,max[n]);

    //php
    $s1_result = $_POST[“s1”];

    My problem is I can’t able to use key s1 morethan once in java (map)

    So is there any other way to do this ?

    Reply
  8. Hi ,
    I am using oauth protocol and need to implement the refresh token policy onvolley. I have written the part where I send a request , generate a token and raise requests using that token but I would love a tutorial on complex parts of volley like how to set retry poilcy if a request fails

    Reply
  9. How to hide json file to browser view becuase any one can use our site json show how to protect….jt from browser view can you explain this

    Reply
  10. when i login the app the user_id is going in database but when we don’t login the app then user_id is not go in databse.how i send the user_id in backend database in every situation using volley in android.

    Reply
  11. Hello Belal Khan, Thanks for this tutorial of android volley.

    Can you show us for this tuto the php script please?? Because I don’t see that value name of array “heroes”.

    And if possible build some tutorial of retrieving data mysql by using or entry “id” like $id=$_POST[‘id’] . PLEASE … HELP ME

    Reply
  12. i m getting same data from every GET request if i insert new data JsonObjectRequest only fech old records not new inserted records

    Reply
  13. Sir, can I get example on displaying one web service on different activies in android? Means displaying json objects on different activies through comparing id’s between them.

    Reply

Leave a Comment