29

My server application exposes a RESTful web service using JAX-RS (Jersey Implementation). What is the best way to invoke this service (other than using Apache HttpClient)? I was wondering whether the REST Client APIs from Jersey, Restlet, RESTeasy and other frameworks work on Android.

Thanks, Theo

Theo
  • 3,074
  • 7
  • 39
  • 54
  • 1
    There's a RESTlet edition for Android. If your server code is based on Jersey so far, there won't be much reusable code. – b_erb Dec 06 '10 at 15:42
  • Googling seems to throw-up plenty of REST clients for android as well as tutorials on writing your own.... – Martijn Verburg Dec 06 '10 at 15:56
  • 4
    @Martijn Verburg Thanks for your comment, but Googling is not the answer to my question. Of course I could build my own using java.net.URL or Apache HttpClient, but I was looking for the best (most convenient) way. E.g. on Android, I would favor Apache HttpClient over java.net.URL for several reasons. And when you have a JAX-RS web service, maybe it's better if you use a client that is also JAX-RS aware saving you from writing boilerplate code and providing better abstractions. – Theo Dec 07 '10 at 07:27
  • You might wanna investigate https://github.com/square/retrofit. It's still a snapshot release, but it looks pretty slick: https://github.com/square/retrofit/blob/master/samples/github-client/src/main/java/com/squareup/retrofit/sample/github/Client.java – Matthew Apr 30 '13 at 20:42

5 Answers5

9

Resteasy-mobile is a perfect solution.

It's basically full blown resteasy (which has client framework) but uses Apache HTTP Client rather than HttpURLConnection (which doesn't exist on android)

Here is more information about usage (http://docs.jboss.org/resteasy/docs/2.3.1.GA//userguide/html_single/index.html#RESTEasy_Client_Framework)

Here is for the maven

    <dependency>
        <groupId>org.jboss.resteasy.mobile</groupId>
        <artifactId>resteasy-mobile</artifactId>
        <version>1.0.0</version>
    </dependency>

A little sample code on android side

    public class RestServices {
    static RegisterSVC registerSVC;
    static PushSVC pushSVC;
    static TrackerSVC trackerSVC;

    RestServices() {
        RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
    }

    public static RegisterSVC getRegisterSVC() {
        return ProxyFactory.create(RegisterSVC.class,"http://143.248.194.236:8080/notification");

    }

    public static PushSVC getPushSVC() {
        return ProxyFactory.create(PushSVC.class,"http://143.248.194.236:8080/notification");
    }

    public static TrackerSVC getTrackerSVC() {
        return ProxyFactory.create(TrackerSVC.class,"http://143.248.194.236:8080/notification");
    }
}

JAX-RS service definition (PushSVC.java) on both android and server side

@Path("/mobile")
public interface PushSVC {
    /*
    Sample
    curl --data '{"collapseKey":"asdf","contentList":{"aaaa":"you","ssss":"you2"}}' -X POST -H 'Content-type:application/json' -v http://localhost:8080/notification/mobile/11111/send
     */
    @POST
    @Path("/{uuid}/send")
    @Consumes(MediaType.APPLICATION_JSON)
    String sendPush( MessageVO message, @PathParam("uuid") String uuid);

}

Model MessageVO definition

public class MessageVO {
    String collapseKey;
    HashMap<String, String> contentList;

    public MessageVO() {
    }

    public MessageVO(String collapseKey) {
        this.collapseKey = collapseKey;
        contentList = new HashMap<String, String>();
    }

    public void put(String key, String value)
    {
        this.contentList.put(key,value);
    }

    public String getCollapseKey() {
        return collapseKey;
    }

    public HashMap<String, String> getContentList() {
        return contentList;
    }
}

This is method invocation on android

public class Broadcast extends AsyncTask<Context,Void,Void>
{

    @Override
    protected Void doInBackground(Context... contexts) {
        MessageVO message = new MessageVO("0");
        message.put("tickerText","Ticker ne` :D");
        message.put("contentTitle","Title ne` :D");
        message.put("contentText","Content ne` :D");
        RestServices.getPushSVC().sendPush(message,TrackInstallation.id(contexts[0]).toString());
        return null;
    }
}

This is pretty simple and all written codes are reusable, boilerplate code is near to non-existence

Hope this help everybody.

Griwes
  • 8,805
  • 2
  • 43
  • 70
Le Duc Duy
  • 1,881
  • 1
  • 19
  • 18
  • 3
    HttpURLConnection is available on Android since version 1. Prior to 2.2 its use was discouraged due to the implementation being buggy, but saying that it "doesn't exist" on Android isn't true. – aaronmarino Apr 01 '14 at 09:19
  • This is by far the best possible solution for java rest back end. nicely plays with rest easy server side implementation. – Rakesh Waghela Feb 03 '15 at 08:31
5

If you want a little bit more comfort than having to deal with URLConnection, check out Resty for Java. Simple, light-weight, but still pretty new.

http://beders.github.com/Resty

Jochen Bedersdorfer
  • 4,093
  • 24
  • 26
  • 3
    I didn't get this running. It requires some classes from api level 9 (2.3), so thats a bad option. – u2ix Feb 15 '11 at 22:09
  • API level 9? Are you sure you commented on the right answer? Let me know how I can help – Jochen Bedersdorfer Feb 16 '11 at 10:58
  • Resty.java imports `java.net.CookieManager` which is in Android since level 9. [API Reference](http://developer.android.com/reference/java/net/CookieManager.html). So I wasn't able to load the class. – u2ix Feb 16 '11 at 12:46
  • 1
    Ah, got ya. Sorry about that. There isn't much I can do about this, since cookies are supported. Thanks for trying and sorry again for the inconvenience this has caused – Jochen Bedersdorfer Feb 16 '11 at 15:43
3

Ran into this one the other day, haven't tried it yet thought, but seems to work on android: http://crest.codegist.org

zhk
  • 161
  • 1
  • 1
  • 5
2

Here is a good example which contains source codes of everything including EJBs, RestEasy and Android:

http://code.google.com/p/android-jbridge/

Mike
  • 21
  • 1
0

For simplest cases you can just use java.net.URL and its openConnection() method to make a request. And then data binding libraries (JAXB for XML, Jackson for JSON) to handle response (and possibly request if you POST xml or json).

StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • 1
    thanks your answer. I was looking for a solution that provides a higher level of abstraction, e.g. like the RESTeasy Client Proxy Framework, where you can reuse your existing JAX-RS Java interface to create a client proxy. But I am not sure whether these "JAX-RS aware" client APIs also work on Android. – Theo Dec 07 '10 at 07:38
  • Understood, sometimes that is nice. Much of convenience however is due to data binding being easy. Anyway, I wish I knew for sure, but it may be worth checking out Jersey client. – StaxMan Dec 07 '10 at 16:45