需要实现Virgil Dobjanschi REST实现模式的Android REST客户端项目样本[英] Need sample Android REST Client project which implements Virgil Dobjanschi REST implementation pattern

本文是小编为大家收集整理的关于需要实现Virgil Dobjanschi REST实现模式的Android REST客户端项目样本的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

REST服务器公开了几个资源,例如(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(思考)我知道如何与REST服务器交谈并获取所需的信息.我将使用像这样的API实现REST客户端类

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 Client应用程序"来自Virgil Dobjanschi我了解到,在活动的工作人员线程中处理其余请求不是一个好主意.相反,我应该使用 service api.

我喜欢拥有绑定到(本地)服务的单身服务helper的想法,但恐怕我不了解服务概念.

目前,我不明白如何将休息呼叫结果报告回到呼叫者活动.我还想知道我是否需要一项处理所有REST请求的服务(带有不同的返回类型),或者我是否需要为每个REST请求提供专用服务.

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

你能告诉我!

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

预先感谢.

klaus

编辑:在SO上找到类似的主题(发布此内容后),这使我朝着我需要的方向(最小化复杂的" dobjanschi模式"):

推荐答案

概述

编辑:

任何人都有兴趣考虑看看 rentful android 让您更好地了解它.

我从试图实施dobjanschi模型的经验中学到的是,并非所有内容都用石头写成,他只会给您概述该怎么做的事情可能会从应用程序转换为应用程序,但该公式是:

遵循此想法 +添加您的=快乐的Android应用程序

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


创建一个具有帐户和帐户管理器的应用程序

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

创建 contentprovider (如果它适合您的需求)

此抽象使您不仅可以访问数据库,还可以访问ServiceHelper以执行REST呼叫,因为它具有与REST ARCH的一个每一对映射方法.

内容提供商| REST方法

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

插入--------------------> put

更新-------------------->发布

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

ServiceHelper Laisering

这个人将基本启动(a)服务,该服务执行HTTP(不一定是协议,但它是最常见的)REST方法,它使用您从ContentProvider传递的参数.我通过了内容提供商的urimatcher获得的匹配整数,所以我知道要访问什么休息资源,即

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,这有什么好处?好吧,请记住,服务在背景中运行是一件好事.

还实施了一个广播员,因此当服务完成时,通知我的活动,该活动再次注册了此广播并需要.我相信最后一步不参加维吉尔会议,但我很确定是一个好方法.

restMethod类

获取参数,WS资源( http://myservice.com/service.com/service.com/service.com/service/path )添加参数,准备所有内容,执行呼叫并保存响应.

如果需要验证,则可以从帐户管理器请求 如果由于身份验证而导致该服务的调用失败,则可以使Authtoken和Reauth无效以获取新的令牌.

最后,RESTMETHOD为我提供了XML或JSON,无论我根据匹配器创建处理器并传递响应.

.

处理器

它负责解析响应并在本地插入.

样本应用程序?当然!

另外,如果您在测试应用程序上很有趣,您可以查看 eli-g 可能不是最好的例子,但它遵循服务休息方法,它是使用ServiceHelper,Processor,ContentProvider,Loader和Broadcast构建的.

其他推荐答案

编程Android 有完整的章节(13.探索内容提供者)专门针对"选项B:使用Virgil的Google I/O Talk中的ContentProvider API".

我们不是唯一看到这种方法的好处的人.在2010年5月的Google I/O会议上,Google的Virgil Dobjanschi发表了一场演讲,概述了以下三种模式,用于使用内容提供商将RESTFULE Web服务集成到Android应用程序中...

在本章中,我们将使用第二个Finch视频示例详细探讨第二个模式;该策略将为您的应用程序带来许多重要的好处.由于这种方法将网络操作集成到Android MVC中,因此我们将其赋予了绰号"网络MVC".

.

编程Android的未来版本可能会解决其他两种方法,并记录本Google演示文稿的更多详细信息.阅读本章后,我们建议您查看Google的演讲.

强烈推荐.

Zigurd Mednieks,Laird Dornin,G.BlakeMeike和Masumi Nakamura编程.版权2011 O’Reilly Media,Inc.,978-1-449-38969-7.

其他推荐答案

"开发Android REST客户端应用程序"由Virgil Dobjanschi引起了很多讨论,因为在会议期间没有提供源代码或之后提供的源代码.

如果您知道更多实施,请评论.

本文地址:https://www.itbaoku.cn/post/627600.html

问题描述

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.

其他推荐答案

Programming Android has a complete chapter (13. Exploring Content Providers) dedicated to 'Option B: Use the ContentProvider API' from Virgil's Google I/O talk.

We are not the only ones who see the benefits of this approach. At the Google I/O conference in May 2010, Virgil Dobjanschi of Google presented a talk that outlined the following three patterns for using content providers to integrate RESTful web services into Android applications...

In this chapter, we’ll explore the second pattern in detail with our second Finch video example; this strategy will yield a number of important benefits for your applications. Due to the elegance with which this approach integrates network operations into Android MVC, we’ve given it the moniker “Network MVC.”

A future edition of Programming Android may address the other two approaches, as well as document more details of this Google presentation. After you finish reading this chapter, we suggest that you view Google’s talk.

Highly recommended.

Programming Android by Zigurd Mednieks, Laird Dornin, G. Blake Meike, and Masumi Nakamura. Copyright 2011 O’Reilly Media, Inc., 978-1-449-38969-7.

其他推荐答案

"Developing Android REST client applications" by Virgil Dobjanschi led to much discussion, since no source code was presented during the session or was provided afterwards.

  • A reference implementation is available under http://datadroid.foxykeep.com (the Google IO session is mentioned under /presentation). It is a library which you can use in your own application.
  • Android Priority Job Queue was inspired by Dobjanschi's talk and sounds very promising to me.

Please comment if you know more implementations.