JSON Parsing Android Studio From URL Example With Step By Step

Hello, developers. Welcome to JSON Parsing Android Studio Example.

In JSON Parsing Android Studio simple example you will learn how to parse simple JSON data from URL step by step.

We will programmatically parse JSON with JSONObject and JSONArray classes in JSON Parsing In Android.

You will get the professional format to call remote Web Services in proper and easiest way at the end of JSON Parsing Android example.

You may find this JSON Parsing Android tutorial little lengthy but once you give it 10 minutes, you will be able to call all web services easily in all your future apps.

First, check the output of JSON Parsing Android tutorial and then we will create it.

Download Source Code for JSON Parsing Android Example

[sociallocker] Download JsonParsing [/sociallocker]

Creating JSON Parsing Android Studio Tutorial step by step

Use Birthday App

We have developed below app which allows you to wish a happy birthday with name and photo on birthday cake to your friend.

Visit app here : https://play.google.com/store/apps/details?id=com.snetchalabs.name_photo_on_birthday_cake

JSON Data Which We Will Parse In JSON Parsing Android Tutorial

How to Parse JSON Data

JSON Parsing Android is not a very big thing.

Mainly there are only two main things in JSON data. First is JSON Object which is indicated by curly brackets { }.

Second is JSON Array which is indicated by square brackets [ ].

JSONObject class (Inbuilt Java Class) is used to parse JSONObject and JSONArray (Inbuilt Java Class) class is used to parse JSONArray.

We will see how to parse these two with special method later in the JSON Parsing Android tutorial. Now go to Android Studio.

Step 1: Create a new project in Android Studio.

Step 2: Updating AndroidManifest.xml file

 add required permissions between <manifest>….</manifest> tag.

Note: If you are targeting SDK version above 22 (Above Lollipop)  then you need to ask a user for granting runtime permissions. Check marshmallow runtime permission for more information.

Final code for AndroidManifest.xml file

Step 3: Adding common classes

We will add some common classes which contain constant variables and public methods.

We can use these variables and methods anywhere in the whole project so that it will reduce data redundancy.

Means that we will write them only once and then we can use them anytime and anywhere when needed.

Names of the classes are:

  1. AndyConstants
  2. AndyUtils
  3. HttpRequest
  4. ParseContent

Step 4: Creating AndyConstants

Create a new Java class and copy following source code

Above class contain URL constants and Parameter constants.

Step 5: Creating AndyUtils

Create a Java class named AndyUtils and add below source code

This class contains a methods to show(showSimplrProgressDialog()) and remove(removeSimpleProgressDialog()) progress dialog when app is fetching JSON data from server.

AndyUtils also includes a method (isNetworkAvailable()) to check whether the Internet of Android device is on or off.

Step 6: Preparing HttpRequest Class

Make new Java class named HttpRequest and paste following source code

We will use methods of this class to establish a connection between an Android device and web server.

Step 7: Creating  ParseContent

Open new Java class and give it a name ParseContent, then Add below source code

In above source code, isSuccess(String response)  method is used to check whether a status of response is true or false (see in JSON data above).

getErrorCode(String responsemethod is used to get the message of JSON data (see in JSON data above).

getInfo(String response) method will parse JSON data. I will describe this method later.

Step 8: Creating Model Class

We will display JSON Data in ListView, so create a new class named “PlayersModel” and add following

Step 9: Creating lv_item.xml layout file

Create a new layout resource file named lv_item.xml file and add following

Step 10: Create one class and name it: CustomeAdapter.java

Copy and paste following code in this class:

Step 11: Copy and Paste following code in activity_main.xml

Step 12: Copy following code in MainActivity.java

Step 13: Description of MainActivity.java

Look at below source code

First, we will check the internet of the device. If it is off, then give message that “Internet is required!”

If internet is on then we will call a web service to get JSON.

Following will call web service.

We will get JSON Data in String Format into variable named “response.”

These JSON Data is sent to onPostExecute(String result) method. Below is code of onPostExecute(String result).

From above method, JSON Data is sent to the onTaskCompleted() method.

JSON Parsing in android is done by getInfo() method of ParseContent class. Following is getInfo() method.

Our JSON data starting from ‘{‘ bracket so it is JSONObject. So the starting ‘{‘ is parse by below line

After that, we have key named “data” and its value is starting from ‘[‘ bracket which is JSONArray. It is parse by following line.

Now data array contains 8 objects so they are parsed by for loop and it is stored in instance of PlayersModel Class.

After getting playersModelArrayList from getInfo() method, listview is populated using custom adapter.

So thats all for JSON Parsing Android Studio example, Feel free to comment your queries and reviews. Thank you 🙂

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

14 Responses

  1. Anam says:

    Really good sample project, it helped me extensively…!

  2. Hardik Parsania says:

    Glad it helped you!

  3. alaaeddin says:

    nice work

  4. Ritvik says:

    Thank you very much

  5. Nik says:

    Well, I looked through several tutorials on this, and this one seems the best so far. Really explained and not only pasted code. However, I am still not able to grasp it completely, I could not make it work with my JSON example. Data cant be read or imported, I am missing something, but with so many pieces around, it is difficult to find where.

    I suppose that is one side of being a programmer? 🙂

  6. Syed kashaf Shakil says:

    I run this program but it is always showing no data

  7. Kerim says:

    Thank’s for this, great work! Work’s pretty well with the main activity, also I was able to adjust params I get from MySQL Database and build the array for your coderequierements.

    I was wondering if it is possible to publicate the results in a fragment… can you help me with this?

  8. Kerim says:

    How can I display this in a fragment?

    • Hardik Parsania says:

      You can use same code for fragment also. code in onCreate() method (activity) should be in onCreateView() method for fragment.

      • Kerim says:

        Alright thank you Hardik!
        Let’s say I want to Toast a Message, when I click a button in the generated ListView… how can I access the data of the clicked list view item?

  9. hamza says:

    I run this program but it is always showing no data, Please can u help me ??

Leave a Reply

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