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.

"<yoastmark

 

 

"<yoastmark

 

"<yoastmark

 

"<yoastmark

 

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
http://192.168.43.17/newprojectapp/create_product.php Create new product name, price, description
http://192.168.43.17/newprojectapp/get_product_details.php Read product details id
http://192.168.43.17/newprojectapp/update_price.php Update product details id, price
http://192.168.43.17/newprojectapp/delete_product.php 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 RequestHandler.java 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 MainActivity.java.

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.

Lets try running our creation!

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.

"<yoastmark

 

"<yoastmark

 

"<yoastmark

 

"<yoastmark

So far we have 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.

5 thoughts on “Android PHP MySQL CRUD operations

Leave a Reply

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