Android应用程序和Web服务器之间同步数据 [英] Sync data between Android App and webserver

查看:180
本文介绍了Android应用程序和Web服务器之间同步数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了一个Android的应用程序,我想它和服务器之间同步数据(如数据库记录,媒体)。如果你看过的Evernote 或类似的应用程序,你肯定明白我的意思。

我有一些问题(假设我们要同步数据库记录):

  1. 每个用户都有为自己的服务器空间的一部分(如的Evernote 的Dropbox )。也许用户创造了新的记录是由手机和服务器创造了新的纪录。 我如何能匹配这些记录在一起?如果有,你建议我什么算法具有相同ID的记录?

  2. 除了 JSON 还有什么办法为手机设备和服务器?

  3. 之间发送数据
  4. 如果 SyncAdapter 的ContentProvider 的可以解决我的问题,请准确解释我。 (如果你能提供一些样品或教程,我或任何意见或关键字,以帮助拓宽/指引我的搜索将AP preciated为好)。

解决方案

我会尽力通过解决更大的问题来回答你所有的问题: 如何同步一个网络服务器和一个Android应用程序之间的数据?


你的网络服务器和一个Android应用程序的

数据同步需要几个不同的组件在Android设备上。

永久存储:

这是手机如何实际存储接收到来自Web服务器的数据。达到这个目的的一个可能的方法是写自己的自定义的的ContentProvider 的一个SQLite数据库的支持。一个体面的教程内容提供者可以在这里找到:<一href="http://thinkandroid.word$p$pss.com/2010/01/13/writing-your-own-contentprovider/">http://thinkandroid.word$p$pss.com/2010/01/13/writing-your-own-contentprovider/

一个的的ContentProvider 的定义一致的接口与存储的数据进行交互。它也可以允许其他应用程序与数据进行交互,如果你想要的。背着你的的ContentProvider 的可能是一个SQLite数据库,缓存,或者任意存储机制。

虽然我一定会推荐使用的的ContentProvider 的使用,你可以使用你想要的任何基于java的存储机制SQLite数据库。

数据交换格式:

这是你用你的网络服务器和你的Andr​​oid应用程序之间发送的数据的格式。两种最流行的格式,这些天是XML和JSON。当选择你的格式,你应该考虑哪些可用的排序序列库。我知道离手,有一个梦幻般的库JSON序列称为GSON: HTTP:// code .google.com / P /谷歌GSON / ,但我敢肯定,类似的库用于XML存在。

同步服务

您会希望某种类型的异步任务,可以从服务器获取新的数据并刷新手机内容,以反映服务器的内容。您还需要每当你进行本地更改的内容,并要反映这些更改通知服务器。 Android提供了的 SyncAdapter 的模式,以此来轻松解决这个模式。您需要注册用户账户,然后机器人将进行大量的魔法给你,并让你自动同步。这里有一个很好的教程:<一href="http://www.c99.org/2010/01/23/writing-an-android-sync-provider-part-1/">http://www.c99.org/2010/01/23/writing-an-android-sync-provider-part-1/


至于如何识别是否记录是相同的,通常你将创建你的Andr​​oid设备和服务器上同时存储唯一ID的项目。你可以用它来确保你指的是相同的。此外,还可以存储列属性,如的updated_at,以确保您始终获得最新的数据,或者你不小心写了新写入的数据。

希望这有助于。

I have developed a application in android that I wanna sync data (such as db record, media) between it and a server. If you've seen Evernote or similar Applications, you certainly understand what I mean.

I have some question (imagine we want to sync DB records):

  1. Every user has a part of server space for himself (such as Evernote or Dropbox). Maybe the user creates new records by cellphone and creates new records in server. How can I match these records together? If there are records with same ID What algorithms do you suggest me?

  2. Except JSON, Are there any way for send data between cellphone device and server?

  3. If SyncAdapter and ContentProvider can solve my problems, please explain exactly for me. (If you could offer some samples or tutorials to me OR Any advice or keywords to help broaden/guide my search would be appreciated as well).

解决方案

I'll try to answer all your questions by addressing the larger question: How can I sync data between a webserver and an android app?


Syncing data between your webserver and an android app requires a couple of different components on your android device.

Persistent Storage:

This is how your phone actually stores the data it receives from the webserver. One possible method for accomplishing this is writing your own custom ContentProvider backed by a Sqlite database. A decent tutorial for a content provider can be found here: http://thinkandroid.wordpress.com/2010/01/13/writing-your-own-contentprovider/

A ContentProvider defines a consistent interface to interact with your stored data. It could also allow other applications to interact with your data if you wanted. Behind your ContentProvider could be a Sqlite database, a Cache, or any arbitrary storage mechanism.

While I would certainly recommend using a ContentProvider with a Sqlite database you could use any java based storage mechanism you wanted.

Data Interchange Format:

This is the format you use to send the data between your webserver and your android app. The two most popular formats these days are XML and JSON. When choosing your format, you should think about what sort of serialization libraries are available. I know off-hand that there's a fantastic library for json serialization called gson: http://code.google.com/p/google-gson/, although I'm sure similar libraries exist for XML.

Synchronization Service

You'll want some sort of asynchronous task which can get new data from your server and refresh the mobile content to reflect the content of the server. You'll also want to notify the server whenever you make local changes to content and want to reflect those changes. Android provides the SyncAdapter pattern as a way to easily solve this pattern. You'll need to register user accounts, and then Android will perform lots of magic for you, and allow you to automatically sync. Here's a good tutorial: http://www.c99.org/2010/01/23/writing-an-android-sync-provider-part-1/


As for how you identify if the records are the same, typically you'll create items with a unique id which you store both on the android device and the server. You can use that to make sure you're referring to the same reference. Furthermore, you can store column attributes like "updated_at" to make sure that you're always getting the freshest data, or you don't accidentally write over newly written data.

Hope this helps.

这篇关于Android应用程序和Web服务器之间同步数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆