Retrieve Data From MySQL Database in Android using Volley

Hey guys, in this post we are going to learn how to Retrieve Data from MySQL Database in Android. And for this we will be using Volley Library. I have already posted a number of …

retrieve data from mysql database in android using volley

Hey guys, in this post we are going to learn how to Retrieve Data from MySQL Database in Android. And for this we will be using Volley Library. I have already posted a number of tutorials about Volley, PHP and MySQL. This post is specifically about retrieving data from MySQL Database in Android. So lets begin.

MySQL Database

So guys this is my database table and I will fetch the data from here.

mysql database

We already have some rows inserted in the above table. And our task here is to fetch those records in our Android Application. If you want you can get my database from below.

MySQL Table

Retrieving Data as JSON from Table

  • Here I am using (XAMPP) so for the PHP side create a folder inside htdocs (C:/xampp/htdocs). I created MyApi.
  • Inside this folder create a file named Api.php and write the following code.

  • Now trying opening this PHP file in your browser. You will see the following output.
Data as JSON from MySQL
Data as JSON from MySQL
  • Now we will use this URL in our android side to get the above JSON data.
  • But remember you cannot use localhost in your URL. You need to find your IP and you can find it using ipconfig command in windows and ifconfig command in linux or mac.
  • In my case the URL is: http://192.168.101.1/MyApi/Api.php

Retrieve Data From MySQL Database in Android

Creating a new Android Project

  • So lets create a new Android Studio project where we will display the data fetched from MySQL.
  • Once your project is created you need to add the dependencies.

Dependencies Required

  • We will use
    • RecyclerView for creating the Product List that we are getting as JSON.
    • Glide to load image from URL.
    • Volley to get the JSON data from URL.

Adding Required Dependencies

  • Add the following lines inside your app level build.gradle file.

  • Now modify the project level gradle file as shown below.

  • Now sync your project and you are done.

Creating Interfaces

  • First we will create a RecyclerView inside our activity_main.xml.

  • Now we will create a new layout resource file named product_list.xml inside the layout folder. This is for our list item.

  • It will create the following output.

recyclerview layout

Creating Model

  • To keep the product we will create a class that will contain all the product attributes. So create a class named Product and write the following code.

Creating Product Adapter

  • Now for RecyclerView we will create a ProductAdapter.java. I already posted about RecyclerView so you can learn about it in detail from this RecyclerView Tutorial.

Fetching JSON and Displaying it in RecyclerView

  • Now the last step is fetching the JSON response from URL and display it to RecyclerView.
  • So come inside MainActivity.java and write the following code.

Adding Internet Permission

  • Lastly add internet permission in your AndroidManifest.xml file.

  • Now you can run your application.
retrieve data from mysql database in android
Retrieve Data from MySQL Database in Android
  • Bingo! It is working absolutely fine.
  • Now if you want you can download my source code from the below given link.

Retrieve Data from MySQL Database in Android Source Code

So thats all for this tutorial friends. Hope you got the point how to retrieve data from mysql database in android. If you are having any confusions or queries you can leave your comments below. Thank You 🙂

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

5 1 vote
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x