[英]raw string in post request body using retrofit
i want to send user:user in the post request body. 我想在post请求体中发送user:user。 I am using retrofit lib.
我正在使用改造 lib。 please suggest me.
请建议我。
I have tried already like this 我已经尝试过这样了
@POST(/login)
void login(@BODY String s,Callback<LoginResponse>)
And called this as 并称之为
login("user:user",Callback<LoginResponse>)
对主体使用TypedString而不是String。
Method #1 方法#1
Consider your "user:user" is seem to a JsonObject. 考虑你的“user:user”似乎是一个JsonObject。 So please post JsonObject instead of Raw String.
所以请发布JsonObject而不是Raw String。
You can construct a POJO class: 您可以构建一个POJO类:
public class User{
public final String user;
User(String user) {
this.user = user;
}
}
Your method like this: 你的方法是这样的:
@POST(/login)
void login(@BODY User user,Callback<LoginResponse>)
And call this as: 并称之为:
User user = new User("john");
login(user,Callback<LoginResponse>)
Since Retrofit uses Gson by default, the User instances will be serialized as JSON as the sole body of the request. 由于Retrofit默认使用Gson,因此User实例将被序列化为JSON作为请求的唯一主体。 Default use content-type: application/json
默认使用content-type: application / json
Method #2 方法#2
You just can post Raw String as Form Data with content-type: application/x-www-form-urlencoded and just need a @FormUrlEncoded annotation. 您只需将Raw String作为表单数据发布,内容类型为: application / x-www-form-urlencoded ,只需要@FormUrlEncoded注释即可。
Here is your method: 这是你的方法:
@FormUrlEncoded
@POST(/login)
void login(@Field("yourkey") String user,Callback<LoginResponse>)
you just need to replace " yourkey " to your api's key - " user " 你只需要将“ yourkey ”替换为api的密钥 - “ 用户 ”
Call this as: 将此称为:
String user = "john";
login(user,Callback<LoginResponse>)
Reference : 参考 :
How to POST raw whole JSON in the body of a Retrofit request 如何在Retrofit请求的主体中发布原始整个JSON
retrofit.http.Field retrofit.http.Field
retrofit.http.FieldMap retrofit.http.FieldMap
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.