本文介绍了需要实现 Virgil Dobjanschi REST 实现模式的示例 Android REST 客户端项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在安卓手机上构建一个 REST 客户端.

I want to build a REST Client on an android phone.

REST 服务器公开了多种资源,例如(获取)

The REST server exposes several resources, e.g. (GET)

http://foo.bar/customer      List of all customer
http://foo.bar/customer/4711    The customer with id 4711
http://foo.bar/customer/vip     List of all VIP customer

http://foo.bar/company           List of all companys
http://foo.bar/company/4711     The company with the ID 4711
http://foo.bar/company/vip      List of all VIP companys

我(认为)我知道如何与 REST 服务器交谈并获取我需要的信息.我会用这样的 API 实现一个 REST Client 类

I (think) I know how to talk to the REST server and get the information I need. I would implement a REST Client class with an API like this

public List<Customer> getCustomers();
public Customer getCustomer(final String id);
public List<Customer> getVipCustomer();

public List<Company> getCompanies();
public Customer getCompany(final String id);
public List<Customer> getVipCompanies();

参考演示《开发Android REST客户端应用程序" 从 Virgil Dobjanschi 我了解到在活动的工作线程中处理 REST 请求不是一个好主意.相反,我应该使用 Service API.

Referred to the presentation "Developing Android REST client applications" from Virgil Dobjanschi I learned that it is no good idea to handle the REST request in an Worker Thread of the Activity. Instead I should use the Service API.

我喜欢拥有一个绑定到(本地)服务的单例 ServiceHelper 的想法,但我担心我没有正确理解服务概念.

I like the idea of having a Singleton ServiceHelper which binds to a (Local) Service but I am afraid that I did not understand the Service concept correct.

目前我不明白如何将 REST 调用结果(在服务中异步完成)报告回调用方活动.我还想知道我是否需要 ONE Service 来处理所有 REST 请求(具有不同的返回类型),或者我是否需要为每个 REST 请求提供专门的服务.

For now I do not understand how to report a REST call result (done asynchrounous in a Service) back to the caller Activity. I also wonder if I need ONE Service which handles all REST requests (with different return types) or if I need a dedicated service for each REST request.

可能我还有很多其他的理解问题,所以对我来说最好的事情是满足我需求的示例应用程序.我的用例并不罕见,我希望那里有示例应用程序.

Probably I have many other understanding problems so the best thing for me would be a sample application which meets my needs. My use case is not unusual and I hope there is in example application out there.

请告诉我!

任何其他为我指明正确实现方向的建议也很有帮助(Android API-Demo 与我的用例不匹配).

Any other suggestions which points me in the correct implementation direction are also helpful (Android API-Demo does not match my use case).

提前致谢.

克劳斯

编辑:在 SO 上发现的类似主题(发布此内容后)引导我朝着我需要的方向前进(最小化复杂的Dobjanschi 模式"):

EDIT: Similar Topics found on SO (after posting this) which lead me in the direction I need (minimizing the complex "Dobjanschi pattern"):

推荐答案

OverView

任何有兴趣的人也可以考虑看看 RESTful android 这可能让你更好地了解它.

Anyone interest also consider taking a look at RESTful android this might give you a better look about it.

我从尝试实施 Dobjanschi 模型的经验中学到的是,并非一切都是一成不变的,他只向您概述了执行此操作的方法可能因应用程序而异,但公式是:

What i learned from the experience on trying to implement the Dobjanschi Model, is that not everything is written in stone and he only give you the overview of what to do this might changed from app to app but the formula is:

遵循这个想法 + 添加您自己的 = 快乐的 Android 应用程序

Follow this ideas + Add your own = Happy Android application

某些应用程序的模型可能因要求而异,有些应用程序可能不需要 SyncAdapter 的帐户,其他应用程序可能使用 C2DM,我最近工作的这个可能对某人有所帮助:

The model on some apps may vary from requirement some might not need the Account for the SyncAdapter other might use C2DM, this one that i worked recently might help someone:

它将允许您使用 SyncAdapter 来同步您的数据.这已在 创建您自己的 SyncAdapter

It will allow you to use the SyncAdapter to synchronized your data. This have been discussed on Create your own SyncAdapter

这种抽象不仅允许您访问数据库,还允许您转到 ServiceHelper 以执行 REST 调用,因为它具有与 REST Arch 的一对一映射方法.

This abstraction allows you to not only access the database but goes to the ServiceHelper to execute REST calls as it has one-per-one Mapping method with the REST Arch.

内容提供者 |REST 方法

Content Provider | REST Method

查询 ----------------> GET

query ----------------> GET

插入----------------> PUT

insert ----------------> PUT

更新----------------> POST

update ----------------> POST

删除---------------->删除

delete ----------------> DELETE

这家伙基本上会启动 (a) 服务,使用您从 ContentProvider 传递的参数执行 Http(不一定是协议,但它是最常见的)REST 方法.我传递了从内容提供程序上的 UriMatcher 获取的匹配整数,因此我知道要访问哪些 REST 资源,即

This guy will basicly start (a) service(s) that execute a Http(not necessarily the protocol but it's the most common) REST method with the parameters that you passed from the ContentProvider. I passed the match integer that is gotten from the UriMatcher on the content Provider so i know what REST resource to access, i.e.

class ServiceHelper{

    public static void execute(Context context,int match,String parameters){
//find the service resource (/path/to/remote/service with the match
//start service with parameters
    }

}

服务

被执行(我大部分时间都使用 IntentService)并且它使用从助手传递的参数进入 RESTMethod,它有什么用?记住服务很适合在后台运行.

The service

Gets executed (I use IntentService most of the time) and it goes to the RESTMethod with the params passed from the helper, what is it good for? well remember Service are good to run things in background.

还实现了一个 BroadCastReceiver,所以当服务完成其工作时,通知我的活动注册了这个广播并再次重新查询.我相信这最后一步不在 Virgill 会议上,但我很确定这是一个很好的方法.

Also implement a BroadCastReceiver so when the service is done with its work notify my Activity that registered this Broadcast and requery again. I believe this last step is not on Virgill Conference but I'm pretty sure is a good way to go.

取参数,WS资源(http://myservice.com/service/path) 添加参数,准备好一切,执行调用,并保存响应.

Takes the parameters, the WS resource(http://myservice.com/service/path) adds the parameters,prepared everything, execute the call, and save the response.

如果需要 authtoken,您可以从 AccountManager 请求如果由于身份验证导致服务调用失败,您可以使 authtoken 和 reauth 失效以获取新令牌.

If the authtoken is needed you can requested from the AccountManagerIf the calling of the service failed because authentication, you can invalidate the authtoken and reauth to get a new token.

最后,无论我基于匹配器创建处理器并传递响应,RESTMethod 都会给我一个 XML 或 JSON.

Finally the RESTMethod gives me either a XML or JSON no matter i create a processor based on the matcher and pass the response.

它负责解析响应并将其插入到本地.

It's in charged of parsing the response and insert it locally.

此外,如果您对测试应用程序感兴趣,请查看 Eli-G,它可能不是最好的例子,但它遵循服务 REST 方法,它是用 ServiceHelper、Processor、ContentProvider、Loader 和 Broadcast 构建的.

Also if you are interesting on a test application you look at Eli-G, it might not be the best example but it follow the Service REST approach, it is built with ServiceHelper, Processor, ContentProvider, Loader, and Broadcast.

这篇关于需要实现 Virgil Dobjanschi REST 实现模式的示例 Android REST 客户端项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:40