如何將Android數據庫與在線SQL服務器同步?

[英]How to Synchronize Android Database with an online SQL Server?


I am developing an Android App that stores different types of data in the built-in SQLite provided by the Android Platform.

我正在開發一個Android應用,在Android平台提供的內置SQLite中存儲不同類型的數據。

Inside the App I have placed a "Sync" button which is supposed to Sync the data between the local SQLite Database, with an Online SQL Server database on my server.

在應用程序中,我放置了一個“Sync”按鈕,用於在本地SQLite數據庫和服務器上的一個在線SQL Server數據庫之間同步數據。

What is the workaround to handle this? This feature can be found in Google Calendar, where you can view the calendar's events on your mobile, and when you add a new event and Sync the data, you can view the updated data by going to your online account too.

如何解決這個問題?這個功能可以在谷歌日歷中找到,您可以在手機上查看日歷上的事件,當您添加一個新事件並同步數據時,您也可以通過登錄在線帳戶查看更新的數據。

Note: I don't want to centralize my database online, because I also want to give the mobile users the ability to use the App without internet connection.

注意:我不想將我的數據庫集中在網上,因為我還想讓移動用戶能夠使用沒有互聯網連接的應用程序。

1 个解决方案

#1


62  

You should take a look at the SampleSyncAdapter project in the Android SDK demos (Transferring Data Using Sync Adapters). It shows you how to do synchronization "Android style", which requires no user interaction by manually tapping a sync button all the time.

您應該在Android SDK演示中查看SampleSyncAdapter項目(使用同步適配器傳輸數據)。它向您展示了如何實現“Android風格”的同步,它不需要用戶通過手動點擊同步按鈕來進行交互。

On top of that you need to write server software that is able to provide your SyncAdapter with a "delta" of all changes since the last commit. The most basic approach is keeping a "last synchronized" time stamp in your app which has to come from the server, otherwise you could get into trouble because of time differences between client and server. Normalize all time stamps as GMT or any timezone of your choice, but stick with that decision. If your app needs to display a time stamp in local time, then you need to use the Java Calendar and TimeZone class for converting the normalized time stamp into local time.

除此之外,您還需要編寫服務器軟件,該軟件能夠為您的SyncAdapter提供自上一次提交以來所有更改的“delta”。最基本的方法是在應用程序中保留一個必須來自服務器的“最后同步”時間戳,否則由於客戶機和服務器之間的時間差異,您可能會遇到麻煩。將所有的時間戳規范化為GMT或你選擇的任何時區,但要堅持這個決定。如果您的應用程序需要在本地時間顯示時間戳,那么您需要使用Java Calendar和TimeZone類來將規范化時間戳轉換為本地時間。

Your app also needs to flag changed local records as "dirty", so your SyncAdapter knows that it needs to upload these changed or new records to the server.

您的應用程序還需要標記已更改的本地記錄為“dirty”,以便SyncAdapter知道需要將這些已更改或新記錄上載到服務器。

Following minimum features are needed for your server software:

您的服務器軟件需要以下最小特性:

  • Update existing record(s) function
  • 更新現有的記錄()函數
  • Add new record(s) function
  • 添加新記錄()函數
  • Get updated records since last synchronization (app provides time stamp)
  • 自上次同步(app提供時間戳)更新記錄
  • Get new records since last synchronization (app provides time stamp)
  • 獲取上次同步后的新記錄(app提供時間戳)

You also may want to read through some Google API (like Google Calendar) for getting an idea of how all of this works and how to design the server API interface for the communication.

您可能還想閱讀一些谷歌API(如谷歌Calendar),了解所有這些如何工作,以及如何為通信設計服務器API接口。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/07/02/72a320426b1c8626dd2aae17f576d6f1.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com