Post 字段采用这种格式并带有 Retrofit

Post fields in this format with Retrofit

所以我需要以这种格式发送一个API请求

{ "access_key": "6477848488cchfc47488", "person": { "first_name": "John", "last_name": "Henry", "email": "john@henry.com" } }

我创建了一个对象

public class Person {
    public String first_name = "";
    public String last_name = "";
    public String email = "";
}

在我的界面中我有

@FormUrlEncoded
@POST("/send_details")
void sendDetails(@Field("person") Person person, @Field("access_key") String accessKey, Callback<User> cb);

最后在我的 Activity 中,我有下面的代码来调用发送详细信息方法

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) {

        }
    });
  }

我收到 500 内部服务器错误。我刚刚从凌空切换到改装。非常感谢任何帮助。

尝试使用 @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;
    }
}

然后:

@POST("/send_details")
void sendDetails(@Body DetailsBody body, Callback<User> cb);

(没有@FormUrlEncoded