Android客户端和REST服务器同步 [英] Synchronize Android client and REST server

查看:275
本文介绍了Android客户端和REST服务器同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含一个Rails服务器:用户和相关的:评论。它被用作一个Android客户后端的API。交换格式加载并在服务器上存储数据是JSON。以下是相关的迁移。

I created a Rails server that contains :users and associated :comments. It is used as a backend API for an Android client. The exchange format to load and store data at the server is JSON. Here are the relevant migrations.

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.timestamps
    end
  end
end

...

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.references :users
      t.string :subject
      t.text :message
      t.timestamps
    end
  end
end

所有用户都已经导入。因此,只读访问配置为:用户资源。因此,对于:评论应该是可以添加新条目。下面是可用的路由。

All users have already been imported. Therefore, only read access is configured for the :users resource. Thus, for :comments it should be possible to add new entries. Here are the available routes.

   user_comments GET   /users/:user_id/comments(.:format)      comments#index
                 POST  /users/:user_id/comments(.:format)      comments#create
new_user_comment GET   /users/:user_id/comments/new(.:format)  comments#new
    user_comment GET   /users/:user_id/comments/:id(.:format)  comments#show
           users GET   /users(.:format)                        users#index
            user GET   /users/:id(.:format)                    users#show

Android客户端

在客户端我用的是服务 AsyncTasks 来下载,分析并存储用户到本地的SQLite数据库。 A 的ContentProvider 提供缓存用户到用户界面。从服务器下载的用户对象包含用户表的唯一ID。当一个新的评论获取客户机上创建这个应该是很有用的。

Android client

On the client side I use a Service with AsyncTasks to download, parse and store users into a local SQLite database. A ContentProvider delivers cached users to the UI. The user object downloaded from the server contains the unique id of the users table. This should be useful when a new comment gets created on the client.

  • 在用户显示在Android客户端上的列表视图。
  • 系统用户项目被选中。
  • 列表活动创建一个意图,其中包含了用户特定的URI,例如: 内容://com.example.myapp.provider/users/23
  • 在用户活动显示有关用户和相关注释详细信息。
  • 在缓存的意见得到通过 CursorLoader 加载。 (1)
  • 从远程服务器
  • 一个同步进程加载的意见。 (2)
  • Users are displayed in a list view on the Android client.
  • A user item gets selected.
  • The list activity creates an Intent which contains the user specific URI, e.g. content://com.example.myapp.provider/users/23.
  • A user activity displays detail information about the user and associated comments.
  • Cached comments get loaded via a CursorLoader. (1)
  • A synchronization process loads comments from the remote server. (2)
  • 注释可从用户活动创建。
  • 注释被存储到本地数据库。 (3)
  • 存储注释sychronized与远程服务器。 (2)

我标志着与以下问题相关的方案步骤。

I marked the scenario steps that are associated with the following questions.

  1. 如何创建内容的URI意见被在用户活动中使用了 CursorLoader ?请介意的话,我只知道用户URI在这一点上。
  2. 有人可以描述我是如何创建一个同步过程?我不知道,如果一个<一个href="http://www.google.com/events/io/2010/sessions/developing-RESTful-android-apps.html">SyncAdapter在这里工作(从来没有使用过)。是同步过程只是一个code>服务是,一方面开始任务下载,分析并存储在客户端上,并在另一方面负载意见,EN codeS并发送意见到服务器?
  3. 如何进行内容的URI一个新的评论是什么样子?是的ContentProvider 征求意见的相同用户?是否有只有一个 SQLiteOpenHelper 应用程序?
  1. How do I create a content URI for the comments being used with a CursorLoader in the user activity? Please mind, I only know the user URI at this point.
  2. Can someone describe how I create a synchronization process? I am not sure if a SyncAdapter works here (never used it). Is the the synchronization process just a Service that on the one hand starts tasks to download, parse and store comments on the client and on the other hand loads, encodes and sends out comments to the server?
  3. How does the content URI for a new comment look like? Is the ContentProvider for comments the same as for users? Is there only one SQLiteOpenHelper in the application?

主要的问题,我挣扎是如何设计的应用程序?如果你知道如何,我应该同步的用户和他们的相关评论一个更好的解决方案,你都非常欢迎。

The main problem I am struggling with is how to design the application? If you know of a better solution on how I should synchronize the users and their associated comments, you are very welcome.

回答以下问题1和3。
我延长了REST模式如下:JSON哈希返回一个的评论的现在包含的相关的用户的的的 ID 的。同样的 ID 的也包括在JSON哈希值的用户的。这两个对象都存储在Android设备上的本地数据库。这让我请求的评论的特定的用户的。我只是通过的服务器用户ID 的是,其中条款。内容的URI的评论的是不是级联,因为我暗示我的问题。它类似于用户的内容URI:

Answers to question 1. and 3.
I extended the REST model as follows: The JSON hash returned for a comment now includes the id of the associated user. The same id is also included in the JSON hash for the user. Both objects are stored into the local database on the Android device. This allows me request comments for a specific user. I simply pass the server user id as WHERE clause. The content URI for comments is not cascaded as I implied with my question. It is similar to the user content URI:

内容://com.example.myapp.provider.commentsprovider/comments

请注意,我改变了字符串的权限的一部分。我决定创建<一个href="http://stackoverflow.com/questions/11975215/how-to-address-multiple-content-providers">separate内容提供商获得的用户评论的。

Note, that I changed the authority part of the string. I decided to create separate content provider for users and comments.

推荐答案

一个简单的架构总是第一个更新的东西在服务器,发送评论张贴到服务器马上和通过的 GCM 用户应该要求更新的评论列表。该流程看起来像:

One simple architecture is to always update things in the server first, sending posted comments to the server right away and from there pushing a notification through GCM to users that should request the updated comments list. The flow would look like:

  • 当应用程序是开放的发送GCM注册ID为您推送通知服务器(比如 uniqush推,或者使用宝石来处理GCM逻辑)自己的服务器,这样一来就可以使用它来发送推送通知给用户,告诉应用程序从服务器更新的意见
  • 建立您最初的高速缓存,只要你想它
  • 每当用户发布一个评论,将其发送到服务器,并与用于创建注释的数据的服务器响应,因此该应用可以使用,并且已经对其进行缓存,如果它想,使用返回的ID和任何其他
  • 在服务器上,当一个评论发布,通过所有相关用户和使用GCM注册ID循环发送推送通知,它可以非常简单,如刚走update_comments: 1
  • 在当推通知由用户抽头该应用中,更新与一个请求的评论缓存到服务器
  • When the app is open send GCM registration id to your push notification server (say uniqush-push, or your own server using a gem to handle the GCM logic), this way you can use it to send push notifications to the user telling the app to update the comments from the server
  • Build your initial cache as you want it
  • Whenever a user posts a comment, send it to the server and make the server respond with the data for the created comment, so the app can use that and already cache it if it wants, using the returned id and whatever else
  • On the server, when a comment is posted, loop through all the concerned users and using the GCM registration id send it a push notification, it could be as simple as having just "update_comments": "1"
  • On the app when the push notification is tapped by the user, update the comments cache with a request to the server

这篇关于Android客户端和REST服务器同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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