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 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.

 

 

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

  • 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

android-sqlite-mysql-sync-example-master.zip

https://github.com/probelalkhan/android-sqlite-mysql-sync-example

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

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 </div></div><!--AfterDocument(59643,17072)--> 
<div class=본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.

번호 제목 글쓴이 날짜 조회 수
93 Android Studio Build시 failed linking references 해결방법 file 졸리운_곰 2020.05.05 600
92 [안드로이드 스튜디오] COULD NOT FIND COM.ANDROID.TOOLS.BUILD:GRADLE:3.0.0-BETA6 file 졸리운_곰 2020.05.05 103
» Android Sync SQLite Database with Server using PHP and MySQL file 졸리운_곰 2019.02.25 7714
90 안드로이드의 MVC, MVP, MVVM 종합 안내서 file 졸리운_곰 2019.01.06 314
89 Getting Started: WebView-based Applications for Web Developers file 졸리운_곰 2018.09.03 355
88 App Inventor - MySQL interface 앱 인벤터 mysql 연결 file 졸리운_곰 2018.04.07 2849
87 Connect App Inventor to MySQL Database 앱 인벤터와 mysql 데이터베이스 연결 file 졸리운_곰 2018.04.07 7158
86 Create an API (PHP) 앱 인벤터와 php 통합 file 졸리운_곰 2018.04.07 578
85 Android WebView javascriptInterface 사용하기 file 졸리운_곰 2018.03.26 558
84 Using the WebViewer Control in App Inventor 앱인터에서 웹뷰 컨트롤 사용 file 졸리운_곰 2018.03.24 473
83 WebView Javascript Processor for App Inventor 앱 인벤터 웹뷰 자바스크립트 인터페이스 file 졸리운_곰 2018.03.24 429
82 android webview로 javascript 호출 및 이벤트 받기(연동하기) file 졸리운_곰 2018.03.19 1352
81 Android Asynchronous Http Client file 졸리운_곰 2017.07.03 680
80 안드로이드 에서 JSON 읽어오기 (JSON parser) file 가을의곰 2017.06.18 797
79 Creating a digital wallet application in App Inventor 2 file 졸리운_곰 2017.05.01 236
78 Android Basic JSOUP Tutorial file 졸리운_곰 2017.03.27 314
77 [Android] 안드로이드 웹페이지 파싱하기 - jsoup 이용하기 file 졸리운_곰 2017.03.27 555
76 Android에서 jsoup를이용하여 HTML 파서(Parser) file 졸리운_곰 2017.03.27 705
75 Android : Keystore 정보확인 및 패스워드 변경 졸리운_곰 2017.03.23 184
74 android studio 2.3 의 안드로이드 레이아웃 배치 법 : Layout Editor로 UI 빌드 file 졸리운_곰 2017.03.09 467
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED