Android JSON Parsing – Getting Data from MySQL Database

This Article is about How to Get JSON Data from MySQL Database into Android App using Android Studio. This is the Simple Light Weight and Easiest way to Retrieve Data from MySQL Database.

So, Here I’m going to Cover Different topics such as Basic of JSON, How to fetch and Retrieve into Android App.

Skip the Topic you Already know about.

What is JSON?

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language. (Wikipedia).

JSON Can be Declared by Two Type.

  1. Object
  2. Array

JSON Object

JSON Object starts with curly brackets and Ends with the Same, It holds the Single Object item.

JSON Array

Where JSON Array starts and ends with Square Brackets and it can hold multiple sets of JSON Objects.

JSON Object Along with JSON Array

This code contains Both JSON array and Object which we are going to use in our code.

Fetching Data using PHP from MySQL Database

Here I’m using XAMPP in my Linux System. You can also use WAMP, LAMPP or anything else.

Create a New Database with Anyname you want and Create Table too.

Look at Example.

Android JSON Parsing -MySQL

You can use the Below Code in-order to Create a Table.

Fetching Database Table Data(s) with PHP in JSON Format

Here we are using PHP to Fetch data from Database and Display in to JSON Format.

Create a New File named api.php & Paste the Below Code.

Then visit http://localhost/FOLDER Name/api.php

Here is how you’ll get the Result.

JSON Parsing from MySQL

As you can see above, We have got our result in JSON Format. (P.S: I’m JSON Handler Chrome Extension to display JSON in Colorful).

Parsing JSON in Android – Android JSON parsing

Creating New Android Studio Project and Choosing Activity

Fire up the Android Studio and Create a New Project and Use name as your choice and Choose Target SDK as your Choice, We are going to Implement Everything on our Own so, Choose EmptyActivity.

My Project Name : JSON Example

My Package Name : json.webcheerz.app

Crafting the UI (User Interface)

I’m changing the Activity Layout to Relative Layout, You can Copy paste the Below Code.

Now it’s time to Fetch the Data from the Server.

Fetching the JSON Data from Server

This is the Core part of our Project. Here We are running the App in Outside of our HOST Machines. So we can’t access it by Localhost.

We Need IP Address to Run our Application.

If you’re using Online Server. You can use Domain Name too.

We Need IP Address to get JSON Data from the Server.

Finding IP Address of the Host Machine.


Finding IP Address in Linux

Open Terminal and Type ifconfig then Press Enter.

Here is my Localhost IP Address. You can check the Below Image.

JSON Parsing from MySQL

Finding the IP Address in Windows

Open CMD (Command Prompt) and type ipconfig  and Press enter. You’ll get the IP Address same as above image.


Accessing the API with IP Address

Before we have used localhost/foldername to access the API, As we are going to use in Remote Device we’ll use IP Address.

Now Access the API using IP Address of the Host Machine.

Here is my API access via Host IP Address

Setting up Internet Permission

Open Mainfest.xml and Add the Below line, above <application> 

Fetching JSON Data from Server

Now we’re going to fetch Data as a JSON String from the MySQL Server through our API.

Here we’re using ASyncTask which let us to Perform Background Activity of Fetching data without affecting the Main Thread(UI).

We are Parsing the JSON from MySQL with the help of API and Showing in ListView inside Android Application.

Setting up Internet Permission

Open Mainfest.xml and Add the Below line, above <application> 

Code Breakthrough

This Below part gets the JSON Data from MySQL Database and stores into the ListView

This Below Code Helps the App Activity to Perform things in Background.
Finally, This this Helps the Fetched JSON Data from MySQL Database to Render into the ListView of Android App.

The Complete MainActivity.java

Open MainActivity.java and paste the Following code below, It carries all the Activities like Fetching the Data & Showing in the ListView Adapter.

Conclusion

When you Run this Application, You’ll see like this below screenshot.

I’m Running it on Real Device, You can Run on AVD Also (Android Virtual Device)

JSON Parsing from MySQL

That’s all for Now !! Will share Source Code to Download soon. 🙂 Thanks.

Was this article helpful?

Thanks

Thanks for your Feeback. It will helps us to improve our Article quality




0 Shares