适用于 Android 的 Java REST 客户端 API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4368047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java REST client API for Android
提问by Theo
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.
我的服务器应用程序使用 JAX-RS(泽西岛实现)公开了一个 RESTful Web 服务。调用此服务的最佳方法是什么(使用 Apache HttpClient 除外)?我想知道 Jersey、Restlet、RESTeasy 和其他框架的 REST 客户端 API 是否适用于 Android。
Thanks, Theo
谢谢,西奥
回答by StaxMan
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, Hymanson for JSON) to handle response (and possibly request if you POST xml or json).
对于最简单的情况,您可以只使用 java.net.URL 及其 openConnection() 方法来发出请求。然后是数据绑定库(用于 XML 的 JAXB,用于 JSON 的 Hymanson)来处理响应(如果您 POST xml 或 json,则可能会请求)。
回答by Jochen Bedersdorfer
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.
如果您想要比处理 URLConnection 更舒服一点,请查看 Resty for Java。简单,重量轻,但仍然很新。
回答by zhk
Ran into this one the other day, haven't tried it yet thought, but seems to work on android: http://crest.codegist.org
前几天遇到了这个,还没有尝试过,但似乎可以在 android 上运行:http: //crest.codegist.org
回答by Mike
Here is a good example which contains source codes of everything including EJBs, RestEasy and Android:
这是一个很好的示例,其中包含 EJB、RestEasy 和 Android 等所有内容的源代码:
回答by Le Duc Duy
Resteasy-mobileis a perfect solution.
Resteasy-mobile是一个完美的解决方案。
It's basically full blown resteasy (which has client framework) but uses Apache HTTP Client rather than HttpURLConnection (which doesn't exist on android)
它基本上是完全成熟的resteasy(具有客户端框架),但使用 Apache HTTP 客户端而不是 HttpURLConnection(在 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)
这里是有关使用的更多信息(http://docs.jboss.org/resteasy/docs/2.3.1.GA//userguide/html_single/index.html#RESTEasy_Client_Framework)
Here is for the maven
这是给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
android端的一些示例代码
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
android 和服务器端的 JAX-RS 服务定义 (PushSVC.java)
@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
模型 MessageVO 定义
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
这是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.
希望这对大家有帮助。