http://www.androiddeft.com/2017/09/10/json-parsing-android-http/


JSON (JavaScript Object Notation) is a syntax used to store and exchange data between the application and the server. It is the best alternative to XML. JSON is easy to read and understand. In this tutorial, we will learn how to retrieve JSON data from the server using HttpURLConnection, parse the JSON and display it in a customized ListVew.

Understanding the JSON Structure

JSON comprises of JSON Objects and JSON Arrays.
JSON Structure

JSON Object:

A JSON object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).

JSON Array:

A JSON array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).

Values:

A value can be another JSON Object, JSON array, String, Number, Boolean value or NULL.  

Creating Project and Adding Permissions

1. Create a Project named JSON Parsing in Android Studio.
2. Create the following package structure.
JSON Parsing Package Structure
3. Add INTERNET permission to AndroidManifest.xml, since we are fetching the JSON data from the server. Open AndroidManifest.xml and add the following line:
  <uses-permission android:name=”android.permission.INTERNET” />

Parsing JSON using HttpJsonParser

Inside utils package, create a class named HttpJsonParser. This class helps in retrieving the JSON data from the server using HttpURLConnection.

Creating EmployeeDetails Class

We will be handling employee data in this tutorial. So let’s create a Java bean called EmployeeDetails inside beans package.

The UI for Displaying the Data

We will have customized List View for displaying the data.

Open activity_main.xml and add a List View.

Now create a file employee_row.xml in layout folder, which contains Text Views to display employee data.

Create a file employee_background.xml inside the drawable folder. This will provide background for each element in the list view.

Creating a ListView using Custom Adapter

In order to display the fetched JSON data from the server, we will be customizing the ArrayAdapter. Create a class EmployeeAdapter in adapter package.

Fetching Individual JSON Nodes

We use the following methods to fetch the JSON nodes:

getJSONArray(String name) : fetches JSON array from JSON object.

getJSONObject(int index) : fetches JSON object from a JSON array.

getString(String name) : fetches String value from JSON object.

getInt(String name) : fetches Integer value from JSON object.

Calling makeHttpRequest from AsyncTask

Any network related operations in Android needs to be performed as an asynchronous task, without disturbing the UI thread. If we perform the network operation on the main thread (UI thread) then we will get NetworkOnMainThreadException. We will be using following methods in AsyncTask:

onPreExecute() : Prerequisite  operations such as displaying progress bar.

doInBackground(String… params) : Performing the network operation (calling makeHttpRequest in this tutorial).

onPostExecute(String result): Parse the response and populate the UI. We will be calling runOnUiThread(Runnable) for the same.

Complete MainActivity class

Below is the complete implementation of MainActivity.

You can download the source code and APK from the below links. You can also watch the demonstration in the below animation. If you have any doubts you can ask them in the comments section.

Download Source CodeDownload APK

JSON Parsing in Android over HTTP

JSON Parsing using Libraries

This tutorial helpful when the JSON structure is simple and data to be processed is small in size. When you want to parse complex and really huge chunk of data, you can make use of libraries

Using Volley Library

Volley HTTP library provides an easy way to make networks calls. In JSON Parsing in Android using Volley, you can learn how to do that same in an easy way using volley networking library which provides you lot of advantages like success/error callback methods and cache options.

Using Retrofit Library

Retrofit is much popular than Volley. The parsing is done using Jackson libraries in which you just have to create models for JSON nodes and the library automatically converts the JSON into appropriate objects. Go through JSON Parsing in Android using Retrofit which explains in detail about the Retrofit library.