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 …

android volley tutorial

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.

[sociallocker id=1372] Android Volley Tutorial – Fetching JSON Source Code[/sociallocker]

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 🙂

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x