Field fields in this format using Retrofit

So I need to send an API request in this format

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

I created an object

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

In my interface there is

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

Finally, in my work, I have the code below to call the data sending method

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 500 internal server errors. I just switched from volleyball to retrofitting. Would thank for any help.

+4
source share
1 answer

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

0

Source: https://habr.com/ru/post/1589155/


All Articles