[英]Post fields in this format with Retrofit
So I need to send an API request in this format 所以我需要以这种格式发送API请求
{ "access_key": "6477848488cchfc47488", "person": { "first_name": "John", "last_name": "Henry", "email": "john@henry.com" } }
{“access_key”:“6477848488cchfc47488”,“person”:{“first_name”:“John”,“last_name”:“Henry”,“email”:“john@henry.com”}}
I have created an object 我创建了一个对象
public class Person {
public String first_name = "";
public String last_name = "";
public String email = "";
}
In my interface I have 在我的界面中我有
@FormUrlEncoded
@POST("/send_details")
void sendDetails(@Field("person") Person person, @Field("access_key") String accessKey, Callback<User> cb);
Finally in my Activity I have the code below to call the send details method 最后在我的Activity中,我有以下代码来调用send details方法
Person person = new Person("John", ":"Henry, "john@henry.com");
aApi.sendDetails(person, ACCESS_KEY, new Callback<User>() {
@Override
public void success(User user, Response response) {
}
@Override
public void failure(RetrofitError error) {
}
});
}
I get a 500 Internal Server Error. 我收到500内部服务器错误。 I've just switched from volley to retrofit.
我刚从排球换到改装。 Would appreciate any help.
非常感谢任何帮助。
Try using @Body
annotation instead of @Field
and passing a single Body
object. 尝试使用
@Body
注释而不是@Field
并传递一个Body
对象。
class DetailsBody {
@SerializedName("access_key")
public String accessKey;
public Person person;
public DetailsBody(String accessKey, Person person) {
this.accessKey = accessKey;
this.person = person;
}
}
and then: 接着:
@POST("/send_details")
void sendDetails(@Body DetailsBody body, Callback<User> cb);
(without @FormUrlEncoded
) (没有
@FormUrlEncoded
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.