Need sample Android REST Client project which implements Virgil Dobjanschi REST implementation pattern

后端 未结 8 591
醉梦人生
醉梦人生 2020-12-04 05:11

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 cus         


        
相关标签:
8条回答
  • 2020-12-04 05:18

    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.

    0 讨论(0)
  • 2020-12-04 05:22

    This is a little late but here is an article which explains the first pattern from the talk:

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

    The thing I like about the first pattern is that the interface to the rest methods is a plain class, and the Content Provider is left to simply provide access to the database.

    0 讨论(0)
  • 2020-12-04 05:23

    Retrofit could be very helpful here, it builds an Adapter for you from a very simple configuration like:

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

    GitHubService service = restAdapter.create(GitHubService.class); Each call on the generated GitHubService makes an HTTP request to the remote webserver.

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

    for more information visit the official site: http://square.github.io/retrofit/

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

    0 讨论(0)
  • 2020-12-04 05:25

    We have developped a library that adresses this issue : RoboSpice.

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

    • executes asynchronously (in a background AndroidService) network requests that will return POJOs (ex: REST requests)
    • caches results (in Json, or Xml, or flat text files, or binary files)
    • notifies your activities (or any other context) of the result of the network request if they are still alive
    • doesn't notify your activities of the result if they are not alive anymore
    • notifies your activities on their UI Thread
    • uses a simple but robust exception handling model
    • supports multiple ContentServices to aggregate different web services results
    • supports multi-threading of request executions
    • is strongly typed !
    • is open source ;)
    • and tested

    We are actually looking for feedback from the community.

    0 讨论(0)
  • 2020-12-04 05:33

    "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.

    0 讨论(0)
  • Good news guys. An implementation of the service helper is available here: https://github.com/MathiasSeguy-Android2EE/MythicServiceHelper It's an open source project (Apache 2). I am at the beginning of the project. I've done a project where I defined the pattern to do, but i haven't yet extract the code to make a clean librairy. It will be done soon.

    0 讨论(0)
提交回复
热议问题