Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java REST client API for Android [closed]

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

like image 306
Theo Avatar asked Dec 06 '10 15:12

Theo


People also ask

Can we use REST API in Android?

This is a REST client, which can be used in Java and Android, to retrieve or upload some data in a certain format (JSON, XML) using a REST-based web service.

What is RESTful API in Android?

A RESTful API is an architectural style for an application program interface (API) that uses HTTP requests to access and use data. That data can be used to GET, PUT, POST and DELETE data types, which refers to the reading, updating, creating and deleting of operations concerning resources.

Can we call REST API from Java?

You can definitely interact with RESTful web services by using URLConnection or HTTPClient to code HTTP requests. However, it's generally more desirable to use a library or framework which provides a simpler and more semantic API specifically designed for this purpose.


2 Answers

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.

like image 141
Le Duc Duy Avatar answered Sep 24 '22 06:09

Le Duc Duy


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

like image 34
Jochen Bedersdorfer Avatar answered Sep 26 '22 06:09

Jochen Bedersdorfer