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.