对于Android的Java REST客户端API [英] Java REST client API for Android
问题描述
我的服务器应用程序,公开使用JAX-RS RESTful Web服务(新泽西州实施)。什么是调用这个服务(除使用Apache的HttpClient等)的最佳方法是什么?我想知道的REST客户端API从泽西岛,的Restlet,RestEasy的等框架是否在Android上正常工作。
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.
谢谢, 西奥
推荐答案
RestEasy的移动是一个完美的解决方案。
Resteasy-mobile is a perfect solution.
它基本上完全成熟RestEasy的(其中有客户端框架),但使用Apache HTTP客户端,而不是HttpURLConnection的(并不在Android上存在)
It's basically full blown resteasy (which has client framework) but uses Apache HTTP Client rather than HttpURLConnection (which doesn't exist on android)
下面是关于用法的更多信息(http://docs.jboss.org/resteasy/docs/2.3.1.GA//userguide/html_single/index.html#RESTEasy_Client_Framework)
Here is more information about usage (http://docs.jboss.org/resteasy/docs/2.3.1.GA//userguide/html_single/index.html#RESTEasy_Client_Framework)
下面是对maven的
<dependency>
<groupId>org.jboss.resteasy.mobile</groupId>
<artifactId>resteasy-mobile</artifactId>
<version>1.0.0</version>
</dependency>
在Android上侧小样本code
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");
}
}
在Android和服务器端的JAX-RS服务定义(PushSVC.java)
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);
}
型号MessageVO定义
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;
}
}
这是Android上的方法调用
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;
}
}
这是pretty的简单,所有书面codeS是可重复使用,样板code附近是不存在
This is pretty simple and all written codes are reusable, boilerplate code is near to non-existence
希望这有助于大家。
这篇关于对于Android的Java REST客户端API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!