[英]How to send x-www-form-urlencoded in a body of POST request using android annotations and resttemplate
my interface looks as follows: 我的界面如下所示:
@Rest(rootUrl = "https://myurl.com", converters = { GsonHttpMessageConverter.class })
public interface CommunicatonInterface
{
@Get("/tables/login")
public Login login(Param param);
public RestTemplate getRestTemplate();
}
The question is what i supposed to put as a param to get in body simply: 问题是我应该简单地将其作为参数放入体内:
login=myName&password=myPassword&key=othereKey
without escaping, brackets or quota. 不得转义,括号或配额。
I've try to pass a string and i just get: "login=myName&password=myPassword&key=othereKey"
but it is wrong because of quota signs. 我尝试传递一个字符串,但我只得到: "login=myName&password=myPassword&key=othereKey"
但是由于配额符号,这是错误的。
If I understand correctly, you want to post login
and password
parameters from a form to your method. 如果我理解正确,则希望将login
和password
参数从表单发布到您的方法中。
For this, you should ensure you have the following steps: 为此,您应确保执行以下步骤:
login
and password
as names. 创建一个登录表单,该表单具有输入文本字段,其中包含login
和password
。 form
has a POST
method, you don't really want to have user's credentials in the URL as get params, but if you use case needs you to do it, you can. 确保form
具有POST
方法,您实际上并不想在URL中使用用户凭据作为获取参数,但是如果用例需要这样做,则可以。 Interface
, instead of using GsonHttpMessageConverter
you should be using FormHttpMessageConverter
. 在你的Interface
,而不是使用GsonHttpMessageConverter
你应该使用FormHttpMessageConverter
。 This converter accepts and returns content with application/x-www-form-urlencoded
which is the correct content-type
for form submissions. 此转换器接受并返回具有application/x-www-form-urlencoded
的content-type
,该content-type
是表单提交的正确content-type
。 Param
class should have fields which have the same name as the input text fields. 您的Param
类应具有与输入文本字段同名的字段。 In your case, login
and password
. 在您的情况下,请输入login
和password
。 After you do this, request parameters posted in the form will be available in the param
instance. 完成此操作后,在表单实例中发布的请求参数将可用在param
实例中。 Hope this helps. 希望这可以帮助。
Example extending LinkedMultiValueMap: 扩展LinkedMultiValueMap的示例:
@Rest(converters = {FormHttpMessageConverter.class, MappingJacksonHttpMessageConverter.class})
public interface RestClient extends RestClientRootUrl {
@Post("/login")
LoginResponse login(LoginRequest loginRequest);
}
public class LoginRequest extends LinkedMultiValueMap<String, String> {
public LoginRequest(String username, String password) {
add("username", username);
add("password", password);
}
}
You can have multiple converters because based on the object passed in, it will choose a converter for you. 您可以有多个转换器,因为根据传入的对象,它将为您选择一个转换器。 That said if you pass in a MultiValueMap it will for some reason add it to the headers because Android Annotations creates a HttpEntity. 也就是说,如果您传入MultiValueMap,由于某种原因,它将被添加到标题中,因为Android注释会创建HttpEntity。 If you extend MultiValueMap as Ricardo suggested it will work. 如果按照Ricardo的建议扩展MultiValueMap,它将起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.