Android JSON Parsing Using Volley Library

In my previous article I gave a brief overview of Volley, including why Volley created, it’s features, Android Networking problems before Volley etc. The length of the tutorial was getting really long so it was not possible to cover all the topics in that article. In this article I will finish what was left and give you a useful working example of Volley.

In this tutorial I will load JSON from network using Volley and populate that data in RecyclerView.

What should you already know?

I’m going to parse this JSON for this project, it looks like,

Now let’s start our project,

First Create a new project from File -> New -> New Project, Name it VolleyDemo & Select Empty Activity (We will build everything from scratch).

1> In your build.gradle file add this dependencies,

2> Create a new Java Class file NetworkController.java

This file will be a Singleton Class of Volley Request Queue. The benefit of making it Singleton is that the Object will last until the end of our application life cycle and every new request will be handle by only one object.

Here is the code, I have commented wherever it’s necessary,

As you can see Inside the private constructor I have created LRU Cache, LRU stands for Least Recently Used, It is in Memory Cache means that the cache will be stored inside the Memory, It has a size limit of 10MB, as soon as the Cache Size will grow more than 10MB, the LRUCache algorithm will remove the least used item and cache the new item. ImageLoader will automatically check for the Image in cache if not available than it will Load Image from URL.

3> In your activity_main.xml paste below code,

 4> Create a new Layout file as singleitem_recyclerview.xml and paste below code,

This is the layout file for our Single List Item.

5>Open colors.xml and paste,

 6>Create a new file NewsFeeds.java,

This file is Simple POJO which will hold values of our JSON,

 7> Create a New file MyRecyclerAdapter.java and write down below code,

This is a Custom Adapter for our RecyclerView which implements ViewHolder pattern, If you are not familier with recyclerview I will suggest to first follow this tutorial.

 8> Finally, in your MainActivity.java write down this,

All code is self explanatory, Follow this tutorial if you don’t know how to make JSON Request using Volley.

 9> Add Internet permission to AndroidManifest.xml,

 

If you have done everything mentioned above the project should work fine. You can experiment using your JSON try to change Cache value, make a simple String request, just get used to it so it will be really easier for you to create something on your own.

If you are having any error or need source code mention below in the comments.

4 comments

  1. vikas shivapuje Reply

    Hi androidnovice

    nice explanation ,helped me lot in understanding volley library example with recycler view
    but from single item recycler view.xml; u remove the root linear layout as it is unnecessary ,because its increaing the gap between cardview on scrolling when we have diffrent size images

  2. Larby Reply

    Can I get the source code of the proyect?

    Is it published in Github.com?

    Thanks

Leave a Reply

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