JSON Parsing in Android using Volley

In my previous post, I have explained how to Parse JSON in Android using HttpURLConnection. In this tutorial, we will see how we can do the same in a simpler way using Volley library.

What is Volley?

Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. You need not create an AsyncTask for running network operation in the background. Volley does this by itself by creating an asynchronous task.

Volley offers the following benefits:

  • Automatic scheduling of network requests.
  • Multiple concurrent network connections.
  • Transparent disk and memory response caching with standard HTTP cache coherence.
  • Support for request prioritization.
  • Cancellation request API. You can cancel a single request, or you can set blocks or scopes of requests to cancel.
  • Ease of customization, for example, for retry and backoff.
  • Strong ordering that makes it easy to correctly populate your UI with data fetched asynchronously from the network.
  • Debugging and tracing tools.

Creating new Project

1. Create a new Project in Android Studio with the name “JSON Parsing Using Volley” and package name com.androiddeft.jsonvolley.

2. Open build.gradle file in the app module and add Volley dependency:

 

3. Add Internet permission to AndroidManifest.xml since we access the JSON data from the server:

 

4. Modifying activity_main.xml

Add 2 buttons: 1 for loading JSON Object and another for loading JSON Array. Have a TextView inside a ScrollView, to display the parsed JSON data.

 

5. In MainActivity.java, create references to the buttons and TextView. Add OnClick events to both the buttons. Declare a method displayLoader() to display the progress bar and have methods loadJsonObj() and loadJsonArray() to load respective JSON data.

 

Creating Singleton Class

Create a class named MySingleton.java. This will help in making use of the Volley library in order to make a new RequestQueue. Details on RequestQueue can be found here: Setting Up a RequestQueue.

 

Making JSON Object Request

For making a JSON Object request, we use JsonObjectRequest class provided by Volley. The response is parsed and the parsed JSON is converted into String and populated to a TextView.

Sample JSON Object Response: http://api.androiddeft.com/volley/json_object.json

 

 

Making JSON Array Request

For making a JSON Array request, we use JsonArrayRequest class provided by Volley. The response is iterated, parsed and the parsed JSON is converted into String and populated to a TextView.

Sample JSON Array Response: http://api.androiddeft.com/volley/json_array.json

 

 

Complete MainActivity Class

 

If you face any issues while implementing, then you can ask them in the comments section. You can see the animation below and download the APK and Source Code.

Download Source CodeDownload APK

JSON Parsing in Android over HTTP

Stay Connected

No spam guarantee.

Abhishek

Abhishek

Abhishek loves coding. His favorite is Android Development. Apart from coding he loves traveling and reading.
Abhishek

Leave a Reply

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