Android PHP MySQL CRUD operations

Hey Technoz, In this post, we will see the how to handle remote database like MySQL from the android app with PHP language as a server side language giving a JSON response. However, this Android PHP MySQL tutorial has two parts: one is server side coding, and another is client side coding.

Android PHP MySQL

Building Custom API

At first, we have to build the custom API (Application Programming Interface) for our application to perform tasks. Apparently,  The app will send the request to the API, the API will interact with the database, perform necessary task, and return the results (JSON response) back to the application. Thus, the following figure will clear how the system will work.

Android PHP MySQL CRUD operations

Hope you get the idea from above figure. At first, We will first build the server side API.

Building Server Side API

Creating Database

We have to build a table required in this Android PHP MySQL tutorial. Use the below SQL query to create the required table.

Creating PHP files

Certainly, the php files does the work of server side with interacting the database. To clarify, I hope you are familiar working with php. If not, please see the series of php tutorials first. I am using Wampserver for this project. So create folder newprojectapp in www directory. At first, we will create the connection file configuration.php.

As a result, we have the table of product details in MySQL database, we will first write the code for adding the new product entry. So create file create_product.php and paste the following code in it.

For instance, The above file accepts the product name, price and description and adds an entry into the database. Therefore, we are here using array to show data and passing JSON response as an array. Similarly, the same mechanism is applied to all files.

Furthermore, create a file get_product_details.php to perform read operation with the following code in android php mysql.

In order to understand update operation, we will update the price of product with specific id. Lets create file update_price.php with the following code.

Finally, the delete operation in this Android PHP MySQL tutorial can be performed from following code. Create php file delete_product.php and paste following code in it.

Lets Test the working

Finally, we have just completed the server side part of Android PHP MySQL tutorial. However, we need to check whether the above code is working as expected. I am here using ARC (Advanced Rest Client) chrome extension to send requests with POST parameters. But you can choose the same, or any other of your choice. Lets try to send requests on all files with the POST data parameters as needed and see the JSON response. See the screenshots below.










Some points about URLs

In this Android PHP MySQL tutorial, the localhost  in the url will not work. Thus, we have to find out the ip address of PC and put it in the place of localhost. To find ip address, open command prompt and type “ipconfig” and press enter. You will get the ip. Copy it and replace in the URLs in your project.

The URLs we are using in this tutorial are specific purpose-related with each sending specific POST parameters. See the table below for clarification.

URL Purpose POST Parameters Create new product name, price, description Read product details id Update product details id, price Delete product details id

Building Client Android App

Subsequently, we are going to build a client side android app. We are using HTTP POST requests. So, open your app-level build.gradle file and paste the following line in ‘android’ component.

Add the Java Backend Code

To sum up, we have only single activity to perform all CRUD operations in android php mysql. We will build a simple java class which will handle the requests and JSON response. create a new java class and paste the following code in it.

Meanwhile, Lets discuss some important points in above code:

We have created a method which accepts the URL and the HashMap parameters which are to be sent along with the URL. (HashMap is a Map based collection class that is used for storing Key & value pairs as HashMap<Key, Value>.) Likewise, We are here using HTTPUrlConnection class to request a url. The method getPostDataString is getting the parameters to be sent and putting them in a single string. After the parameters are sent on specific URL, we check if the response is successful, read it, combine the JSON response as a single string and return to the calling function.

Designing the Layout

Furthermore, we will move towards the activity in android php mysql. To create a front view, create a new activity, if you don’t have already and paste the following code in the layout xml file. In my case, it is activity_main.xml.

The above layout file has components required to fulfill our purpose of this android php mysql tutorial. Lets cover the java part in

Note: In the above class, we are using the URLs to send the API request. You have to replace the IP addresses in these URLs with your device’s IP address. To know your IP address, connect your PC to internet, open command prompt, type ipconfig/all and press enter. See the IPv4 Address and copy it. Then replace that address in the above URLs. (If you are getting End of input error, then you must check this note)

As you see, in the above class, the functions for the CRUD operations are created and are triggered on clicking the buttons in xml file.

In each method above,we have created a private class which extends an AsyncTask. An AsyncTask is a class that allows us to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. On button click, we have to perform some operations like getting values from EditTexts, mapping them in the HashMap, setting the TextViews, etc. we will do these things in the background. Above all, It has two implementation methods in our Android PHP MySQL tutorial, described as follows:

onPreExecute(): It performs the task on UI thread. We here start the flashing of ProgressBar.

doInBackground(Params… params): This method starts as soon as onPreExecute() is finished. It performs the major tasks. In this case, it attaches parameters to HashMap and we then call the sendPostRequest() method in RequestHandler class with URL and HashMap parameters. It returns the response to the onPostExecute() method.

onPostExecute(Result result): It receives the response from doInBackground() method and executes again on UI thread. Here we do the task of dismissing the ProgressBar.

However, we are getting our JSON response from server, in the onPostExecute() method, we use the JSONObject class and retrieve the Strings from the response using getString() method.

Finally, after the completion of class, we create the object of class and execute it.

Add Internet Permission

As we are using the internet for our project, so we have to add the internet permission in AndroidManifest.xml file as follows:

Put this line just before starting of <application> tag.

Lets try running our creation!

Note: Please make sure that you have changed the IP address in your url with your own IP address. Also note that while running the project, you need to have WAMP or XAMPP server running and your PC should be connected to the internet in order to obtain the IP address.

Thus, We have completed the Android PHP MySQL tutorial. Now try running the app. If everything goes well, then you will see a success screenshots of the app as follows.








So far we are done with everything. Hope you understood everything. Alternatively, If you want to jump directly to the implementation, you can download the source code from the below link.

Download Source Code


If you have any problem implementing above tutorial, feel free to ask in the comments below. Please subscribe to our newsletter in order to receive such knowledgeable tutorials.

32 thoughts on “Android PHP MySQL CRUD operations

    1. P
      Patricio says:

      Change SdkVersion in build.gradle (module) add api ‘’ in dependencies section

      apply plugin: ‘’

      android {
      //Change Version
      compileSdkVersion 28
      defaultConfig {
      applicationId “cl.linuxcode.bibliorest”
      minSdkVersion 19
      targetSdkVersion 28
      versionCode 1
      versionName “1.0”
      testInstrumentationRunner “”
      buildTypes {
      release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile(‘proguard-android-optimize.txt’), ‘’
      compileOptions {
      sourceCompatibility = ‘1.8’
      targetCompatibility = ‘1.8’
      buildToolsVersion = ‘28.0.3’

      dependencies {
      implementation fileTree(dir: ‘libs’, include: [‘*.jar’])
      implementation ‘’
      implementation ‘’
      testImplementation ‘junit:junit:4.12’
      androidTestImplementation ‘’
      androidTestImplementation ‘’
      implementation “”
      implementation ‘androidx.annotation:annotation:1.1.0’
      implementation ‘’
      // add API
      api ‘’


      Download Sources Files of this page, uncompress and copy folders and files

  1. alan yu says:

    I also get the error as well. It seems the code have some bugs..

    Exception: org.json.JSONException: End of input at character 0 of.

    any help?

    1. Ashish Joshi says:

      You need to replace the ip address with yours, in the url in the code. Also, you need to have WAMP/XAMPP server running and your PC must be connected to internet while running the project. If you have any more questions, let me know. I’ll try to help.

      1. alan yu says:

        It worked well in ARC, but when I clicked the submit/go/update price/delete product button in android app, it always show up

        Exception : org.json.JSONException:End of input at character 0 of

        p.s. I already replaced the ip address to my ipv4 address

    1. Ashish Joshi says:

      Please check the logcat if it shows any error. Also, your device must have active internet connection as the work needs ip address of device. Alternatively, you can download the source code from above link. If that doesn’t solve your problem, you can contact by filling contact form on this website.

  2. R
    Ricardo Macir Gotola says:

    Hello Friend,

    I am trying to make the crud work, but I have the same problem as the friends above, I realized that who is generating this error is the AppCompatActivity class, it returns the “s” as empty.

    I would like to make an attempt to install exactly the technology you used, from API to android, do you remember?

    thank you in advance, Ricardo

    1. Ashish Joshi says:

      Please see the above solutions in comments and make sure you have tried them all. You can also download the source code directly and try to use it in your project. If any issue still persists, then you can use contact form of this website to describe your issue, or email to, and attach the screenshot of your logcat.

    1. Ashish Joshi says:

      For image, you can add a new field in database with image URL and modify the api file to send response back to the app along with that URL. Then parse the image URL from the response and then on android side, you can download the image and display it by creating an ImageView.

  3. M
    Mar says:

    Hi. I’m a beginner in android php mysql development and I saw your great tutorial thanks to that.
    Im trying to use the the php file on it and I’m getting an error like this

    Notice: Undefined index: name in C:\wamp64\www\newprojectapp\create_product.php on line 8
    Call Stack
    # Time Memory Function Location
    1 0.0005 404992 {main}( ) …\create_product.php:0

    {“error”:true,”message”:”Insufficient parameters”}

    Hope you can help me with this. Thanks in advance!

  4. d
    demi says:

    hello, I keep having
    W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method ‘int java.lang.String.length()’ on a null object reference

Leave a Reply

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