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 🙂

71 thoughts on “Retrieve Data From MySQL Database in Android using Volley”

  1. Error:(1, 0) Cause: com/android/build/gradle/AppPlugin : Unsupported major.minor version 52.0
    Open File

    this my error please help i open the project to android studio

    Reply
  2. When I rotate the screen, the data gets reloaded. How to solve that ? Is there any way to implement Loaders as is done in AsyncTask to avoid reloading of data.

    Reply
    • you need to save data before goes to “On Create” when you use a save data in OnSavedInstanceState.

      ///onSaveInstanceState
      @Override
      protected void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
      outState.putInt(“TagDataToSave”,DataToSave);
      }

      ////OnCreate Code

      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      if(savedInstanceState!=null)
      {
      TagDataToSave=savedInstanceState.getInt(“DataToSave”);

      }
      else
      {
      TagDataToSave=10;//To OnCreate on a first time
      }
      }

      Reply
  3. in this list if i want to perform filter with user data like user will click on a option then that data will goes to server and give a response on this list.plz help me

    Reply
  4. am not getting any error but not displaying the values from the database to the recyclerview but the php file is displaying the data on the browser

    Reply
  5. Hi man thanks a lot for the tutorial u are awesome.
    Would u help me with this one.
    I want to write an application that fetches data from database using either Volley or Okhttp.
    So what i want to achieve is a multi list view.
    1.first list view is for fetching albums with images and text
    2.second list view Fetches songs under a particular album–they also contain images and text
    3.third screen gives full information of a song –they also contain images and text

    Can u do a tutorial on this one. Thanks your help.

    Reply
  6. I want to retrieve only the image that too by email as a login profile so, should i use array in php to fetch the image?

    Reply
  7. Hi,
    Thanks for your tutorial. I am getting below error :
    org.json.JSONException: Value >>>”+jsonObject.toString(), Toast.LENGTH_LONG).show();
    // Log.i(TAG,”Status >>”+jsonObject.getBoolean(“status”));

    if (jsonObject.getBoolean(“status”)) {
    Toast.makeText(MainActivity.this, “Success occurred..\n” + jsonObject.toString(), Toast.LENGTH_LONG).show();
    }else{
    Toast.makeText(MainActivity.this, “Else occurred..\n” + jsonObject.toString(), Toast.LENGTH_LONG).show();
    }
    } catch (JSONException e) {
    e.printStackTrace();
    }

    }
    Please help.

    Reply
  8. Thanks for the code Sir …
    But what if i want to use spinner and select one category and fetch data for specific column according to the selected category?? please answer me sir
    Thanks in Advance

    Reply
  9. E/dalvikvm: Could not find class ‘android.graphics.drawable.RippleDrawable’, referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering

    error aa raha he

    Reply
  10. Hey, can i get data from online MySQL server(means from my online website database)?
    if yes, what link i will give in the code??
    you have given ip address of computer, what will be the link i will add in code to access data from database??
    Waiting for your reply.

    Reply
  11. Hi,
    I have this error:
    error: class ProductsAdapter is public, should be declared in a file named ProductsAdapter.java

    Reply
  12. Hi,

    Thank’s for your tutorial but I have a problem by fetching the datas.

    The products array seems to be created and the temp one also. The php scripts walks through the “while”, but the either the array_push or the last echo don’t show me any result.

    Another problem is that I’m in France and the accents like “é” don’t appear in the fields.

    May you help me ?

    Reply
  13. am not getting any error but not displaying the values from the database to the recyclerview but the php file is displaying the data on the browser why is it so

    Reply
  14. Hi, thank you for the tutorial. Really appreciate it. Can you help me please? I’ve gone through the tutorial and it works fine, but I’d like filter the recyclerview based on store_id, for example: a list of products but only view products by selected store…

    Reply
  15. Hello Sir,I m new to MySQL actually I want to ask that should my PC be on all the time if I fetch data or upload that to my database.

    Reply
  16. please I have really learnt a lot from your post and must say its great. now i need some help am developing an app
    1. user enter – let say : location and region in different edit text views and submits query
    2. result should display all hospitals in that location and region in a list view but a radio button form and check box.
    3. from here the user has to select one and press a button to send it.
    4. before the activity is sent, it is sending to the select hospitals email address
    5. so the app so be able to select to email of the selected hospital and by pressing the submit button an email is sent to the hospital.

    can you please guide me to achieve this task thanks

    Reply
  17. Hi, when I change the phone language to Arabic nothing display just the image, return to the English language show all data.
    I can’t find where the error is. Mysql database is utf8_unicode_ci , how can I Retrieve Data and display it in Arabic.

    Reply
  18. Hey there it’s a great tutorial thanks for that, but here I stucked an error occurs that is, “E/RecyclerView: No adapter attached; skipping layout” whenever I run my code in an physical device, but when I run my code in emulator it’ s work fine so what is the reason behind that and what is the solution?, Please help!

    Reply
  19. The Data is being displayed but it says in the logcat that “No Adapter Attached; Skiiping Layout” . I am stuck for days on this one please help me! Thank you…

    Reply
  20. To start with, I want to say thank you for these amazing Tuts, but my question is:

    Suppose I want to view the data from MYSQL (The One I have displayed on my Android Device) offline, how can I modify the code above.

    I would also appreciate if you can point me to the actual tut that has this information.

    My Main goal is to create an app that synchronises both ways (maintaining a perfectly synched and updated database in MYSQL & SQLite and either of the changed on these database should be seemlessly appended to them both)

    Reply
  21. hi thanks,
    If a deceiving person decompile the apk, it will access this get json data URL , which means accessing all json data inside the database. what’s the solution?

    Reply
  22. Error:Execution failed for task ‘:app:packageAllDebugClassesForMultiDex’.
    > java.util.zip.ZipException: duplicate entry: android/support/v4/hardware/display/DisplayManagerCompat.class

    when i build the project it is fine but when i run it, it shows this error, please any solution for this

    Reply
  23. Thanks Belal for help.
    It Worked for me after removing some Exception.. ?.

    But I Am Facing One Issue With My API.php It’s Running Well On LocalHost But When I Upload It To My Online Server Its Just Run For First Time And Returning The Same Result Again And Again .. PLEASE HELP ME .

    Thank You.

    Reply
  24. Hello, I am having a problem with the function execute on the Api.
    Its giving me this message>

    Fatal error: Uncaught Error: Call to a member function execute() on bool in C:\xampp\htdocs\MyApi\api.php:23 Stack trace: #0 {main} thrown in C:\xampp\htdocs\MyApi\api.php on line 23
    How I can fix this?

    Reply
  25. E/RecyclerView: No adapter attached; skipping layout

    Sir,I m getting this error although I have set the adapter.

    Reply
  26. how can you select specific item from database and load them in the recyclerview
    like select from * tablename where phone number = the user specify in the android app

    Reply
  27. incompatible types: int cannot be converted to String
    StringRequest stringRequest = new StringRequest(Request.Method.GET, BASE_URL,

    the method GET is Int. can solve my error?

    Reply

Leave a Comment