Android Sync SQLite Database with Server using PHP and MySQL

Lets learn Android Sync SQLite Database with Server. Assume we have to send some information from the application to our webserver and internet is not available on the device at a particular time. So instead of …

Android Sync SQLite Database with Server

Lets learn Android Sync SQLite Database with Server. Assume we have to send some information from the application to our webserver and internet is not available on the device at a particular time. So instead of giving error to the user that internet is not available we can store the data to SQLite and send it later automatically when the internet is available. And this is what we are going to learn in this Android Sync SQLite Database with Server Tutorial.

Android Sync Sqlite Database with Server Demo

  • You can first see what we are going to learn in this video where I am showing the application.

  • Now lets move ahead and start Android Sync Sqlite Database with Server.

Creating Web Service and MySQL Database

Creating Database

  • I have the following database. I am using XAMPP you can use anything you want.

mysql database

  • So we have the database table. Now we will create a php script that will handle the insertion to the database.

Creating Web Service

Creating Script

  • Create folder in your root directory (in my case it is htdocs).
  • Now create a php file inside the folder, I have created saveName.php. And write the following code.

Testing Script

  • Now its time to test the script we created. So in my case the URL is http://localhost/SqliteSync/saveName.php
  • I am using POSTMAN to test the script and you can see it in below screenshot.

postman

  • As you can see the script is working fine. Now lets move ahead in android project.

Android Sync SQLite Database with Server

Creating Android Project

  • Create a new project.
  • I have created AndroidMySQLSync with an Empty Activity.

Adding Permissions

  • We need the following permissions so first add these to AndroidManifest.xml.

Adding Dependencies

  • For network requests I am going to use Volley. So add the following line inside dependencies block of your app level build.gradle file.

Handling SQLite Operations

  • In this case we have to use both SQLite and MySQL. So a class named DatabaseHelper.java and write the following code.

Handling Volley RequestQueue

  • We are going to use Volley for http request. So for this we will create a singleton class.
  • Create a class named VolleySingleton and write the following code.

Building Interface

MainActivity

  • Now inside activity_main.xml write the following code.

  • The above code will generate the following output.
Android Sync SQLite Database with Server
Android Sync SQLite Database with Server
  • As you can see we have an EditText, a Button and a ListView.
  • Now let me tell you what we are going to do. We will save the Name from EditText and we will also display the saved name in ListView with the Sync Status.  So the next part is designing a layout for our Custom ListView.

ListView

  • Create an xml file inside layout directory. I have created names.xml.

  • As you can see we have a TextView to display the name and an ImageView to display the status.
  • Download the icons from the below link, we have two images to display queued or synced.

[download id=”4043″]

  • You have to copy the downloaded icons inside drawable folder.

Building ListView

Model Class

  • Now create a class Name.java and write the following code.

  • Now we will create an Adapter for our ListView.

Adapter

  • Create a class NameAdapter.java and write the following code.

Coding MainActivity

  • Now lets come inside MainActivity.java and write the following code.

  • Now if you will run the application it will save the name to MySQL and SQLite with the sync or unsynced status.
  • But to send the unsynced names to the server automatically we have to detect the Network Status of the phone. For this we need one more broadcast receiver.

Detecting Network State

Creating Broadcast Receiver

  • Create a class named NetworkStateChecker.java and write the following code.

Adding Receiver in Manifest

  • Add the following code in your AndroidManifest.xml file inside application tag.

Registering Receiver

  • You also need to register the receiver. So add the following line inside onCreate() method of your MainActivity.java file.

  • Now you can run your application.

Testing the Application

  • Run your application. And try saving the name when internet is available also turn off the internet and again try saving your name.

    android sync sqlite database with mysql
    Android Sync SQLite Database with Server
  • When the internet will be available again the data will be automatically sent to MySQL.

Download Source Code

  • You can get the source code from the following GitHub repository.

[sociallocker id=1372] Android Sync SQlite Database with Server Source Code [/sociallocker]

So thats it for this android sync sqlite database with server tutorial friends. Feel free to leave your comments if you are having any troubles making the project. Also follow the steps carefully as the post is very long. And if you found this helpful please favor us by sharing this post in your social network. Thank You 🙂

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x