We need a sample Android REST Client project, which implements a sample implementation of Dobzhansky REST Peak

I want to create a REST Client on an Android phone.

A REST server provides several resources, for example. (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 apply 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(); 

Refers to the presentation " Development of REST client applications for Android " from Vergil Dobzhansky I found out that you should not process a REST request in a working topic. Instead, I should use the Service API.

I like the idea of ​​having a Singleton ServiceHelper that binds to a (local) service, but I'm afraid that I did not understand the correct concept of the service.

So far, I do not understand how to report the result of a REST call (executed asynchrounous in the service) back to the caller's action. I also wonder if I need one service that processes all REST requests (with different types of returned data), or if I need a dedicated service for each REST request.

Perhaps I have many other problems with understanding, so the sample application that suits my needs would be the best for me. My use case is not unusual, and I hope there is an example application there.

Could you tell me!

Any other suggestions that point me in the right direction of implementation are also helpful (Android API-Demo doesn't match my use case).

Thanks in advance.

Klaus

EDIT . Similar topics were found on SO (after the publication of this), which lead me in the direction I need (minimizing the complex "Welcome" template):

  • Android: customer service <
+79
java android rest design-patterns
Feb 09 '11 at 17:13
source share
8 answers

Overview

Edit:

Any interest is also considering taking a look at RESTful android , this may give you a better overview.

What I learned from experience in trying to implement the Dobjani model is that not everything is written in stone, and it gives you a general idea of ​​what to do, this can change from application to application, but the formula:

Follow these ideas + Add your own = Happy Android App

The model in some applications may differ from the requirement that may not be needed for the account for the SyncAdapter, another may use C2DM, this one that I recently worked on may help someone:




Create an application that has an Account and AccountManager

This will allow you to use the SyncAdapter to synchronize your data. This has 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 also access the ServiceHelper to make REST calls, because it has a one-on-one mapping method using REST Arch.

Content Provider | REST Method

request ----------------> GET

Paste ----------------> PUT

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

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

ServiceHelper Lamination

This guy will usually start (a) the services that perform the HEST (not necessarily the protocol, but this is the most common) REST method with the parameters that you passed from the ContentProvider. I passed the integer number of matches that was received from UriMatcher in the Content Provider, so I know which REST resource I need 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 } } 

Service

It executes (I use IntentService most of the time) and it goes to RESTMethod with the parameters passed from the helper, why is this useful? well remember that the service is good for running things in the background.

Also implement BroadCastReceiver, so when the service is completed, its work will notify my activity, which again registered this broadcast and request. I believe this last step is not at the Virgil conference, but I'm sure this is a good way.

Class RESTMethod

It takes parameters, the WS resource ( http://myservice.com/service/path ) adds parameters, prepares everything, makes a call and saves the response,

If authtoken is required, you can ask the AccountManager if the service call failed due to authentication, you can cancel authtoken and reauth to get a new token.

Finally, RESTMethod gives me either XML or JSON, regardless of whether I create the processor based on the connector and send the response.

CPU

It charges from parsing the response and inserts it locally.

Sample application? Sure!

Also, if you are interesting in a test application, you are looking at Eli-G , this may not be the best example, but it follows Service REST, it is built with ServiceHelper, Processor, ContentProvider, Loader and Broadcast.

+49
04 Oct 2018-11-11T00:
source share

Android programming contains a full chapter (13. A study of content providers) devoted to “Option B: Using the ContentProvider API” from Virgil Google I / O.

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

In this chapter, study the second sample well in detail with our second Finch video sample; This strategy will provide several important benefits for your applications. Thanks to the elegance with which this approach combines network operations in Android MVC, weve given him the nickname "Network MVC".

In a future release of Android Programming, two other approaches may be considered, as well as more information about this Google presentation. After you finish reading this chapter, we suggest that you review the posts on Google.

Highly recommended.

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

+16
Jan 24 '12 at 17:52
source share

The development of REST client applications for Android by Virgil Dobrzanski led to a lot of discussion because the source code was not presented during the session or was subsequently provided.

Please comment if you know more implementations.

+11
Jan 01 '11 at 15:45
source share

We have developed a library that refers to this problem: RoboSpice .

The library uses the “service approach” described by Virgil Dobzhansky and Neil Goodmann , but we offer a complete all-in-one solution that:

  • runs asynchronously (in the background AndroidService) requests that will return a POJO (for example: REST requests)
  • caches results (in Json or Xml, or text files or binary files)
  • notifies your activity (or any other context) about the result of the network request if they are still alive
  • does not notify the result of your actions if they are no longer alive
  • notifies your activity of their user interface
  • uses a simple but robust exception handling model
  • supports multiple ContentServices to aggregate various web services results
  • supports multithreaded query execution
  • strongly typed!
  • is open source;)
  • and tested

We are really looking for feedback from the community.

+7
01 Oct
source share

Upgrading can be very useful here, it creates an adapter for you that has a very simple configuration, for example:

Retrofitting turns your REST API into a Java interface.

 public interface GitHubService { @GET("/users/{user}/repos") List<Repo> listRepos(@Path("user") String user); } 

The RestAdapter class generates an implementation of the GitHubService interface.

 RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint("https://api.github.com") .build(); 

Service GitHubService = restAdapter.create (GitHubService.class); Each call to the generated GitHubService makes an HTTP request to the remote web server.

 List<Repo> repos = service.listRepos("octocat"); 

For more information, visit the official website: http://square.imtqy.com/retrofit/

Note : the RestAdapter adapter that you get from Retrofit is not derived from BaseAdapter , you have to do a wrapper for it somehow like this SO question. Why is my ListView empty after calling setListAdapter inside the ListFragment?

+4
Apr 13 '14 at 10:18
source share

You must check the source code for the official I / O 2010 , for starters, especially SyncService and the various classes in io subpackage .

+3
Feb 09 '11 at 17:25
source share

This is a bit late, but here is an article that explains the first template from the conversation:

http://www.codeproject.com/Articles/429997/Sample-Implementation-of-Virgil-Dobjanschis-Rest-p

What I like about the first template is that the interface for the rest of the methods is a simple class, and the content provider is simply granted access to the database.

+3
Jul 29 2018-12-12T00:
source share

Good guys. The implementation of the service assistant is available here: https://github.com/MathiasSeguy-Android2EE/MythicServiceHelper This is an open source project (Apache 2). I am at the beginning of the project. I did a project where I defined a template, but I have not yet extracted the code to make pure librairy. This will be done shortly.

0
Jan 28 '13 at 17:51
source share