Home » Volley Library Fetching JSON Data from URL

Volley Library Fetching JSON Data from URL

by Online Tutorials Library

Volley Library Fetching JSON Data from URL

In this tutorial, we will fetch the JSON data from the URL using Volley library. Volley is an HTTP Library which provides the facilities for the network connectivity of our app.

The advantages of using the Volley library are as follows:

  • More comfortable and faster request management.
  • It provides efficient network management.

Android Volley Fetching JSON Data from URL Example

In this example, we will load the JSON data from the URL using Volley library. The JSON data contains the String “name”, String “imageurl” and String “description” of tutorials. After fetching the data from the URL, they are displayed in ListView. You can refer more about JSON parsing tutorial at https://tutoraspire.com/android-json-parsing-tutorial.

Let’s create a JSON data which contains the following information.

JSON data at URL (provide your URL of information) = http://192.168.1.35:8080/jsondata/

The JSON data looks like as:

Volley Library Fetching JSON Data from URL


Create an activity_main.xml in layout and add the following code.

activity_main.xml


Now, create a list_item.xml file in the layout directory which contains the row items for ListView. This contains one ImageView for image display and two TextView for text display.

list_item.xml


Create a data model class with name Tutorial.java and information String “name”, String “imageurl” and String “description”.

Tutorial.java


Create a custom adapter class named as MyAdapter.java and extend ArrayAdapter to handle the custom ListView. In this class we are calling an anonymous class new ImageDownloaderTask(holder.imageView).execute(imageUrl) to download images from URL.

MyAdapter.java


Add the following library files in the build.gradle file.

build.gradle


Creating an anonymous class ImageDownloaderTask.java which extends AsyncTask<>. This class downloads (or fetch) the images from URL while executing doInbackground() method and returns the result (bitmap) to onPostExecute(). In onPostExecute() method, the bitmap are set to the ImageView.

ImageDownloaderTask.java


In MainActivity.java class, we are fetching and parsing the JSON data from the URL using Volley.

MainActivity.java

Add the Internet permission in AndroidManifest.xml


AndroidManifest.xml

Output:

Volley Library Fetching JSON Data from URL

Next TopicAndroid Linkify

You may also like