Send Data from Android to MySQL Using Volley POST Request

In the previous tutorial, you have learned the very basics of Web Services in Android. In this tutorial, you will learn how to populate MySQL database using POST request method. You will also learn the utilization of the Volley Library in Android.

Introduction to Volley

The Volley is a modern HTTP networking library for Android which provides a faster and an easier way of communication with the web server for data synchronization.

How it Works?

In Android, the Volley library makes a request to the web server for data synchronization. It is similar to the three-way handshake; the Android application makes some HTTP request, and the server sends some response in return.

Getting Started with Volley

As we want to send some data to the webserver using POST request and store the data in MySQL. So the first thing you will need is MySQL Database on the web server.

There are multiple free web servers available. Hostinger.co.uk provides the best free domains and hosting, but unfortunately, it is not providing its services in Pakistan. Many of you think of going toward the base.pk for free domain and hosting. I suggest you not to use base.pk as it generates an unknown JavaScript error. It may waste a lot of your time. This JavaScript error is generated due to Ads as well as some data parsing which comes as a response from the web server. A better option is to buy your domain and hosting to save time.

Creating MySQL Database

After logging into your hosting CPanel, find and open MySQL Databases and create a new database as required. Also, set up and assign a privileged user to perform operations on the database.

Create and assign users to the database
Create and assign users to the database

 

Now add tables to the database and add columns/fields to the tables in MySQL from phpMyAdmin. 

Add Columns to MySQL Database
Add Columns to MySQL Database

Create a Database Connection File

You have successfully created the database. You need a PHP connection file which will help you to connect to the database to perform CRUD operations.

dbConnect.php

Right now you need the insertion of data in MySQL so only implement the Insert operation in a separate file which checks whether the requested method is a post method or not. If the requested method is POST, it gets the variables from the request and updates them in the database.

insertData.php

You have completed with the server side configurations; The directory path on the server for the insertData.php will act as the responder for the requests from Android application, as it checks the request method and executes the server side query for data insertion in the database. In my case the URL is

http://beginnersheap.com/android/insertData.php

Keep in mind that if your website has SSL certificate even then you must keep the URL http://example.com/insertData.php. Otherwise, it will generate the error.

Now move toward the Android Application.

Populate MySQL from Android Application using Volley

Before you start building the components of Android application, you must configure Volley library and its dependencies immediately after creating your Android application.

Adding Internet Permissions to Android Application

In the AndroidManifest.xml file, place the <uses-permission /> self closing tag between the <manifest> </manifest> tags, add internet permission.

 Including Volley Library to Android Application

Open the build.gradle (Module: App) add a dependency for Volley Library, inside dependencies block place a Volley library compile code.

The complete dependencies code will be.

You have setup Volley, Sync Project to synchronize your build.gradle file with your project.

Setup User Interface for Getting Data from the User

Create the required User; I took username and email address as an example to send two Strings as a POST request.

Create text fields as required and a button to send the POST request for saving data in MySql.

Volley Project TextFields and Button
Volley Project TextFields and Button

The Complete Code for User Interface of Volley Project

Implement the onClick method for the button

Use private static final fields for the URL and the column names as these fields must not change throughout the project.

I declared username and email as the column/fields name. So, I will declare URL, USERNAME and USEREMAIL as public static final with the class level scope.

Get Data From Text Fields

Before moving further you must get the values from the text fields which user entered.

Request Methods

Create a StringRequest listener which will override onResponse() and onErrorResponse() methods along with getParams() method which return a HashMap containing key-value pair.

onResponse() method will returns the response from the web server and onErrorResponse() method will return the Volley errors.

Generate Requests and Add Request to the Queue

Generate the requests and add to RequestQueue

Working Volley Library POST Request
Working Volley Library POST Request

These user credentials will be saved into MySQL database.

Data from Android in MySQL
Data from Android in MySQL

Complete Code for ActivityMain.java

Hope you like this tutorial, Stay tuned for more upcoming tutorials. Stay blessed!

  • Alvina Adam

    very easy and understandable really it helps me

  • Vismay Patil

    What if you have more than two fields?