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

번호 제목 글쓴이 날짜 조회 수
101 나인패치(Nine Patch) 이미지란? file 졸리운_곰 2020.10.10 54
100 Python으로 안드로이드 앱 만들기 졸리운_곰 2020.09.30 839
99 [Android] Fragment 를 이용한 탭 만들기 file 졸리운_곰 2020.09.19 52
98 빠르게배우는 안드로이드 Fragment-4(Fragment간 에 데이터전달) file 졸리운_곰 2020.09.19 36
97 빠르게배우는 안드로이드 Fragment-3(Fragment기초 실습) file 졸리운_곰 2020.09.19 58
96 빠르게배우는 안드로이드 Fragment-2(Activity-> Fragment로 쉽게 변경) 졸리운_곰 2020.09.19 38
95 빠르게배우는 안드로이드 Fragment -1 (배경) file 졸리운_곰 2020.09.19 38
94 안드로이드 스튜디오 - 새로 생성한 Activity Class를 쉽게 Manifest에 등록하기. file 졸리운_곰 2020.08.08 52
93 Android Studio Build시 failed linking references 해결방법 file 졸리운_곰 2020.05.05 517
92 [안드로이드 스튜디오] COULD NOT FIND COM.ANDROID.TOOLS.BUILD:GRADLE:3.0.0-BETA6 file 졸리운_곰 2020.05.05 43
» Android Sync SQLite Database with Server using PHP and MySQL file 졸리운_곰 2019.02.25 7388
90 안드로이드의 MVC, MVP, MVVM 종합 안내서 file 졸리운_곰 2019.01.06 255
89 Getting Started: WebView-based Applications for Web Developers file 졸리운_곰 2018.09.03 264
88 App Inventor - MySQL interface 앱 인벤터 mysql 연결 file 졸리운_곰 2018.04.07 2320
87 Connect App Inventor to MySQL Database 앱 인벤터와 mysql 데이터베이스 연결 file 졸리운_곰 2018.04.07 5290
86 Create an API (PHP) 앱 인벤터와 php 통합 file 졸리운_곰 2018.04.07 493
85 Android WebView javascriptInterface 사용하기 file 졸리운_곰 2018.03.26 497
84 Using the WebViewer Control in App Inventor 앱인터에서 웹뷰 컨트롤 사용 file 졸리운_곰 2018.03.24 390
83 WebView Javascript Processor for App Inventor 앱 인벤터 웹뷰 자바스크립트 인터페이스 file 졸리운_곰 2018.03.24 365
82 android webview로 javascript 호출 및 이벤트 받기(연동하기) file 졸리운_곰 2018.03.19 1299
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED