我创建了一个包含:users和关联的Rails服务器:comments.它用作Android客户端的后端API.在服务器上加载和存储数据的交换格式是JSON.以下是相关的迁移.
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.timestamps
end
end
end
Run Code Online (Sandbox Code Playgroud)
...
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
Run Code Online (Sandbox Code Playgroud)
所有用户都已导入.因此,仅为:users资源配置了读访问权限.因此,:comments应该可以添加新条目.这是可用的路线.
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
Run Code Online (Sandbox Code Playgroud)
在客户端,我使用Servicewith AsyncTasks来下载,解析并将用户存储到本地SQLite数据库中.A ContentProvider将缓存的用户提供给UI.从服务器下载的用户对象包含users …