与Android注释REST服务POST请求 [英] POST request with Android Annotation REST Service

查看:218
本文介绍了与Android注释REST服务POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的Andr​​oid注释在我的项目,并尝试发送 POST 通过以下code的请求,但没有在以下code作为我没有得到预期的反应有些不对劲:

  @Rest(使用rootUrl =htt​​p://xyz.com,转换器= {} GsonHttpMessageConverter.class)
公共接口A {    @Post(/身份验证/电子邮件/)
    公共对象attemptLogin(地图数据);
}

其中,数据(键,值)对。有什么我很想念也许我必须设置请求头数据不应该是JSON?


解决方案

我发现从的 REST客户端使用Android的注解


  

的GET请求一样,它是非常简单的发送采用Android的注解POST请求。一个区别是,你需要定义你要发送一个自定义类(例如事件类在下面的例子),或者如果你想动态控制这个参数,然后一个Map(如MultiValueMap)。该请求的URL仍然可以在使用封闭的内部{...}变量和响应类似的方式来构造,可以类似地处理如在GET请求


  @Post(/事件)
无效的addEvent(事件事件);@Post(/事件/(编号))
无效addEventById(事件事件,长ID);@Post(/身份验证/事件/)
对象authenticateEventData(MultiValueMap数据);

I am using Android Annotation in my project and trying to send POST request through following code, however there is something wrong in following code as I am not getting response as expected:

@Rest(rootUrl = "http://xyz.com", converters = {GsonHttpMessageConverter.class})
public interface A {

    @Post("/authenticate/email/")
    public Object attemptLogin(Map data);
}

Where data is (key, value) pair. Is there anything I am missing perhaps Do I have to set request-header or data should not be JSON?

解决方案

I found the solution from Rest client using Android-Annotations.

Like the GET requests, it is extremely simple to send POST requests using Android-Annotations. One difference is that you need to define the parameters that you are going to send as a custom class (e.g. Event class in the example below) or if you want to control this dynamically, then a Map (e.g. a MultiValueMap). The url for the request can still be constructed in a similar fashion using the variables enclosed inside {...} and the response can be handled similarly as in GET requests.

@Post("/events")
void addEvent(Event event);

@Post("/events/{id}")
void addEventById(Event event, long id);

@Post("/authenticate/event/")
Object authenticateEventData(MultiValueMap data);

这篇关于与Android注释REST服务POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆