Volley with Kotlin and PHP to Insert data in MySQL database

Hi guys, Here is another tutorial. We are going to learn how to use Volley with Kotlin and PHP to Insert data in MySQL database. We shall develop android application using kotlin that allow user to record weekly activity which are saved in MySQL database. Volley library with transmit android data over the network, PHP will receive volley data and Insert it in MySQL database to be stored.

  • let’s start coding
  • Creating MySQL database

  • We are going to use MySQL database that comes with XAMPP.
  • Ensure you have XAMPP installed you can download it form apachefriends
  • Open your xampp and start both Apache and MySQL.
  • Open your browser and type localhost/phpmyadmin.
  • Create a new database and name it weeklyactivity.
  • Select your database and click SQL on the top bar of the page.
  • SQL

  • copy and paste the code below in it.
  • You will have the following output.
  • MySQL database

    Creating PHP files

  • We are going to create 2 PHP files.
  • First one is db.php file for connecting to database, Second is addActivity.php file for adding activity and days to the database.
  • We will use PDO “PHP DATA OBJECT” for connecting to the database.
  • Open your directory C:/xampp/htdocs.
  • Make a folder and name it activity.
  • Inside that folder create a file and name it db.php “C:/xampp/htdocs/activity/db.php“.
  • Using your best code editor copy and paste the code below in db.php file.
  • You can change database credential if yours doesn’t match with the one below
  • Create another file in activity folder and name it addActivity.php “C:/xampp/htdocs/activity/addActivity.php“.
  • This is the file that receives from android.
  • Copy and Paste the code below to your addActivity.php
  • Now we are done on web part. We get started on android part.
  • Creating New Kotlin Application

  • Open your android studio click File => New Project.
  • Give it a Name and Remember to check on Include kotlin support.
  • New Kotlin Application

  • Click Next = > Empty activity => Finish.
  • Adding Volley Library to our project

  • Volley library is used for transmitting network data.
  • We will use volley to help us pass data from android app to our MySQL database.
  • You can learn more about from here
  • Include the code below in your build.gradle file .
  • Kotlin Dependancy Structure

    Creating our Layout file

  • Your layout activity will look as shown below.
  • Volley Kotlin Layout

  • Open your activity_main.xml and paste the code below.
  • Defining Spinner Array Data

  • We are going to define our spinner data in our string.xml found under values folder.
  • Open String.xml file copy and paste the code below.
  • Creating Volley library Singleton Kotlin File/Class

  • To improve volley efficiency we will create VolleySingleton class.
  • This class will help use to encapsulate RequestQueue and other volley functionality.
  • Create a Kotlin File/Class by right clicking on Java folder => New => Kotlin File/Class and name it VolleySingleton.
  • Open VolleySingleton and paste the code below.
  • Intializing Volley and Sending Data

  • Open your MainActivity file.
  • Copy and Paste the code below.
  • That’s all, Run your app and you will see the output below.
  • Volley with Kotlin and PHP to Insert data in MySQL database

  • Now open your phpmyadmin, You will see your activity have been added.
  • MySQL added Activity from Volley

  • To download the source code click here
  • If you have any question or need help comment below.