如何使用android注释和resttemplate在POST请求的主体中发送x-www-form-urlencoded

Vic*_*cek 6 java spring android resttemplate android-annotations

我的界面如下所示:

@Rest(rootUrl = "https://myurl.com", converters = { GsonHttpMessageConverter.class })
public interface CommunicatonInterface
{
@Get("/tables/login")
public Login login(Param param);
public RestTemplate getRestTemplate();
}
Run Code Online (Sandbox Code Playgroud)

问题是我应该把它作为一个简单的参数进入体内:

login=myName&password=myPassword&key=othereKey
Run Code Online (Sandbox Code Playgroud)

没有逃避,括号或配额.

我试图传递一个字符串,我只是得到: "login=myName&password=myPassword&key=othereKey"但由于配额标志,这是错误的.

Aks*_*hay 1

如果我理解正确,您希望将表单中的参数发布到您的方法中loginpassword

为此,您应该确保执行以下步骤:

  1. 创建一个登录表单,其中包含带有loginpassword作为名称的输入文本字段。
  2. 确保form有一个POST方法,您实际上并不希望在 URL 中将用户的凭据作为 get 参数,但如果您的用例需要您这样做,则可以。
  3. 在你的Interface, 而不是使用GsonHttpMessageConverter你应该使用FormHttpMessageConverter. 该转换器接受并返回application/x-www-form-urlencoded适合content-type表单提交的内容。
  4. 您的Param类应该具有与输入文本字段同名的字段。就你而言,login并且password. 执行此操作后,表单中发布的请求参数将在param实例中可用。

希望这可以帮助。