需要品尝的Andr​​oid REST客户端项目,该项目实现了维吉尔Dobjanschi REST实现模式 [英] Need sample Android REST Client project which implements Virgil Dobjanschi REST implementation pattern

查看:99
本文介绍了需要品尝的Andr​​oid REST客户端项目,该项目实现了维吉尔Dobjanschi REST实现模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android手机上建立一个REST客户端。

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

其余服务器公开了多种资源,如(GET)

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的服务器,并获得我需要的信息。我想实现一个REST客户端类像这样的API

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();

中提到的presentation开发Android REST客户端应用程序从维吉尔Dobjanschi我才知道,那是没有什么好主意来处理活动的工作线程REST请求。相反,我应该使用服务的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.

我喜欢有一个Singleton 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调用的结果(在服务完成 - 异步)返回给调用者的活动。我也想知道,如果我需要一个服务负责处理所有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,演示不符合我的使用情况)。

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"):

推荐答案

编辑:

任何人的利益也考虑采取看看 REST风格的Andr​​oid 这可能会给你一个更好的看吧。

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:

按照此思路+添加您自己=快乐的Andr​​oid应用程序

Follow this ideas + Add your own = Happy Android application

在一些应用程序的模型可能需要改变一些可能不需要的帐户可能使用C2DM的SyncAdapter等,这其中,我最近的工作可以帮助别人:

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拱门。

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

这家伙会basicly启动(一)服务(S),其执行HTTP(不一定是协议,但它是最常见的)REST方法,你从ContentProvider的传递的参数。我通过比赛整数从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会议,但我pretty的肯定是一个很好的路要走。

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 )添加参数,prepared一切,在执行调用,并保存响应。

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 AccountManager If 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.

另外,如果你在你礼-G 一个测试应用程序有趣的,它可能不是最好的例子但它遵循服务REST方法,它是建立与ServiceHelper,处理器,ContentProvider的,装载机和广播。

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.

这篇关于需要品尝的Andr​​oid REST客户端项目,该项目实现了维吉尔Dobjanschi REST实现模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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