需要示例Android REST Client项目,实现Virgil Dobjanschi REST实现模式 [英] Need sample Android REST Client project which implements Virgil Dobjanschi REST implementation pattern

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

问题描述

我想在Android手机上构建REST客户端。



REST服务器公开了几个资源,例如(GET)

  http://foo.bar/customer所有客户的列表
http:// foo。酒吧/客户/ 4711客户的身份为4711
http://foo.bar/customer/vip所有VIP客户的列表

http://foo.bar/company全部列表公司
http://foo.bar/company/4711该公司的ID为4711
http://foo.bar/company/vip所有VIP公司的列表

我(想)我知道如何与REST服务器通话并获取我需要的信息。我将使用这样的API实现一个REST客户端类

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

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

参考演示文稿开发Android REST客户端应用程序来自Virgil Dobjanschi我了解到,在工作中处理REST请求并不是个好主意活动线程。相反,我应该使用服务 API。



我喜欢有一个绑定到(本地)服务的Singleton ServiceHelper的想法,但是我不明白服务概念是否正确。



现在我不明白如何将REST调用结果(在服务中完成异步)返回给调用者Activity。我也想知道我是否需要一个处理所有REST请求(具有不同返回类型)的ONE服务,或者我需要为每个REST请求提供专用服务。



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



请让我知道!



任何其他指示我正确执行方向的建议也是有帮助的(Android API-Demo与我的用例不符)。



提前感谢。



Klaus



编辑:找到相似的主题在SO(发布之后)导致我在我需要的方向(最小化复杂的Dobjanschi模式):




解决方案

OverView



编辑:



有兴趣还可以考虑看看 RESTful Android ,这样可能会给你一个更好的看看吧。



从尝试实施Dobjanschi模型的经验中我学到了一点,并不是所有东西都是用石头写的,他只给你概述这个可能从应用程序改变到应用程序的方式,但公式是:



按照这个想法+添加自己的= Happy Android应用程序



某些应用程序上的模型可能会有所不同根据需求,有些可能不需要SyncAdapter帐户,其他可能使用C2DM,我最近工作的一个可能会帮助某人:






< h2>创建具有Account和AccountManager的应用程序

它将允许您使用SyncAdapter来同步数据。已经在创建自己的SyncAdapter 讨论了这一点。



创建 ContentProvider (如果适合您的需要)



这个抽象使您不仅可以访问数据库,还可以访问ServiceHelper来执行REST调用,因为它具有REST Arch的一对一映射方法。



内容提供商| REST方法



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



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



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



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



ServiceHelper分层



这个人将基本上开始(a)执行Http(不一定是协议,但最常见的)REST服务从ContentProvider传递的参数。我通过内容提供者从UriMatcher获得的匹配整数,所以我知道要访问的REST资源,即

  class ServiceHelper {

public static void execute(Context context,int match,String parameters){
//找到服务资源(/ path / to / remote / service,匹配
//开始服务参数
}

}



服务



获取执行(大部分时间都使用IntentService),并且从助手传递的参数转到RESTMethod,它有什么好处?



同时实现一个BroadCastReceiver,所以当服务完成后,它的工作通知我的活动,再次注册了这个广播和重新查询。这最后一步不是在维京会议上,但我确定是一个很好的方法。



RESTMethod类



拿p参数,WS资源( http://myservice.com/service/path )添加参数,准备好所有内容,执行调用,并保存响应。



如果需要authtoken,您可以从AccountManager请求
如果服务的调用失败,因为身份验证,您可以使authtoken和reauth无效以获得新令牌



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



处理器



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



A样品申请?当然!



另外,如果您在测试应用程序中感兴趣,请查看 Eli-G ,它可能不是最好的例子,但它遵循Service REST方法,它是由ServiceHelper,Processor,ContentProvider,Loader和Broadcast构建的。


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

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

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

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.

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.

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.

Would you please let me know!

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

Thanks in advance.

Klaus

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

解决方案

OverView

Edit:

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

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:

Follow this ideas + Add your own = Happy Android application

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:


Create an application that have Account and AccountManager

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

Create a ContentProvider (if it suits your needs)

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.

Content Provider | REST Method

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

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

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

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

ServiceHelper Layering

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 
    }

}

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.

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.

RESTMethod class

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

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.

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

The processor

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

A Sample Application? Of course!

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.

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

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