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.

7 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

  3. Rahul Reply

    in my case when i adding volly dependencies it showing me error could not find volly…

  4. Ismail Osunlana Reply

    this code works perfectly, but there are little bugs.
    1. there are large spaces between each cardviews in the recycler view, so set the linearlayout
    when you scroll to the end aand you start scrolling back, the last cardview items repeats itself several times, to fix it,your linearlayout which is the parent of the cardview, set the layout_width to “wrap content”

    2. when you scroll to the end of the recyclerview and you start scrolling back up, the last cardview would repeat itself several times before scrolling up. to fix this, you need to edit your adapter codes, let it look like this.

    package com.motivational.videos.sposh;

    /**
    * Created by user on 11/1/2017.
    */

    import android.content.Context;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;

    import com.android.volley.toolbox.NetworkImageView;

    import java.util.List;

    /**
    * Created by AndroidNovice on 6/5/2016.
    */
    public class MyRecyclerAdapter_featured extends RecyclerView.Adapter {

    private List feedsList;
    private Context context;
    private LayoutInflater inflater;

    public MyRecyclerAdapter_featured(Context context, List feedsList) {

    this.context = context;
    this.feedsList = feedsList;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View rootView = inflater.inflate(R.layout.singleitem_recyclerview_featured, parent, false);
    return new MyViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
    NewsFeeds_featured feeds = feedsList.get(position);
    //Pass the values of feeds object to Views
    holder.setTitle(feeds.getFeedName());
    holder.setContent(feeds.getContent());
    // holder.setImage(feeds.getImgURL(), NetworkController_featured.getInstance(context).getImageLoader());
    holder.setRating(String.valueOf(feeds.getRating()));

    }

    @Override
    public long getItemId(int position) {
    return position;
    }

    @Override
    public int getItemViewType(int position) {
    return position;
    }

    @Override
    public int getItemCount() {
    return feedsList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

    private TextView content, title, ratingbar;
    private NetworkImageView imageview;

    public MyViewHolder(View itemView) {
    super(itemView);
    title = (TextView) itemView.findViewById(R.id.title_view_featured);
    content = (TextView) itemView.findViewById(R.id.content_view_featured);
    // Volley’s NetworkImageView which will load Image from URL
    imageview = (NetworkImageView) itemView.findViewById(R.id.thumbnail_featured);
    ratingbar = (TextView) itemView.findViewById(R.id.ratingbar_view_featured);
    ratingbar.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    // Toast.makeText(context, “Rated By User : ” + feedsList.get(getAdapterPosition()).getRating(), Toast.LENGTH_SHORT).show();
    }
    });
    }

    public void setTitle(String s) {
    title.setText(s);
    }
    public void setContent(String s) {
    content.setText(s);
    }
    public void setRating(String s) {
    ratingbar.setText(s);
    }

    }

    }

Leave a Reply

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