我有一个REST服务,并希望与Android一起使用它。服务(JAX-RS)发布JSON数据。所以主要的问题是:在Android上消费REST服务,最简单的方法是什么?
- 是否有一个好的现成的Android解决方案,如果没有,您还能推荐些什么?
- 我想转移POJO,我怎么能意识到这一点? GSON是一种合适的方法吗?
谢谢
我有一个REST服务,并希望与Android一起使用它。服务(JAX-RS)发布JSON数据。所以主要的问题是:在Android上消费REST服务,最简单的方法是什么?
谢谢
由于API等级8,你可以使用AndroidHTTPClient
,或者您可以使用DefaultHttpClient
没有问题,并且使用HttpPost
或HttpGet
发送数据更早的API。对于JSON
中的编码数据,是的GSON
是一个好方法,但我认为org.json
会更容易使用。
Spring Android有一个RestTemplate,非常易于使用。
http://static.springsource.org/spring-android/docs/1.0.x/reference/html/rest-template.html
例如:
String url = "http://mypretendservice.com/events";
RestTemplate restTemplate = new RestTemplate();
Event[] events = restTemplate.getForObject(url, Event[].class);
RestEasy的移动是一种完美的解决方案(https://github.com/tdiesler/resteasy-mobile)
它基本上完全成熟RestEasy的(它有客户端框架),但使用Apache HTTP客户端而不是HttpURLConnection(在android上不存在)
这里是更多的信息关于使用(http://docs.jboss.org/resteasy/docs/2.3.1.GA//userguide/html_single/index.html#RESTEasy_Client_Framework)化
下面是行家
<dependency>
<groupId>org.jboss.resteasy.mobile</groupId>
<artifactId>resteasy-mobile</artifactId>
<version>1.0.0</version>
</dependency>
在机器人侧小样本代码
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服务定义(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);
}
型号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;
}
}
这是在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;
}
}
这是非常简单的,所有写代码是可重复使用的方法调用,样板代码是附近不存在
希望这有助于每一个人。
谢谢。 org.json以何种方式更易于使用?在这两种技术中只需要几行代码? – ceran 2012-02-14 09:48:01
也许这只是我的看法,但我更喜欢JSON。它也包含在Android SDK中,所以不需要额外的库管理器。 – 2012-02-14 09:53:08
hm我以为GSON是用于将对象序列化为/从JSON序列化的,它不是JSON本身的替代方案。我弄错了吗? – ceran 2012-02-14 11:11:58