Akhil Latta
Akhil Latta

Reputation: 1723

Retrofit POST request with a complex JSON

I am new to Retrofit and trying to make a POST request using Retrofit. My POST request has a body which is complex JSON like this:

payload :

{
    "user": {
        "personal_info": {
            "first_name": "Walter",
            "last_name": "White",
            "date_of_birth": "1972-01-03 00:00:00 PST"
        },
        "address_info": {
            "address_1": "308 NEGRA ARROYO LANE",
            "address_2": "#",
            "city": "ALBUQUERQUE",
            "state": "NM",
            "zip_code": "87104"
        },
        "contact_info": {
            "email": "[email protected]",
            "telephone": "88888888888",
            "telephone_type_id": 1
        },
        "employment_info": {
            "employer_name": "SELF",
            "employment_status_id": 7,
            "annual_income": 9000000000000000,
            "monthly_income": 750000000000,
            "employment_start_year": 0,
            "employment_start_month": 0
        }
      }
    }

I have the interface defined as

    @POST("/users")
    void registerUser(@Body User User, Callback<User> cb);

I am not sure how my User class should look like, I want the payload to be same as mentioned above, so far I have my User class as

public class User {


    @SerializedName("personal_info")
    private PersonalInfo personalInfo;
    @SerializedName("address_info")
    private AddressInfo addressInfo;
    @SerializedName("contact_info")
    private ContactInfo contactInfo;
    @SerializedName("employment_info")
    private EmploymentInfo employmentInfo;

}

I am not sure if Personalinfo, ContactInfo etc should be separate classes with all the fields mentioned in JSON request or should I just have them as inner class in the User class and how to make the Retrofit call with User class. Server response is exactly similar to the JSON request mentioned above with 1 additional field i.e User Id. I have the POJO classes ready to get the response.

I am confused on how to make the POST request. Thanks in advance for help.

Upvotes: 4

Views: 3272

Answers (2)

rciovati
rciovati

Reputation: 28073

A User object when serialized will have this JSON representation:

{
    "personal_info": {
        "first_name": "Walter",
        "last_name": "White",
        "date_of_birth": "1972-01-03 00:00:00 PST"
    },
    "address_info": {
        "address_1": "308 NEGRA ARROYO LANE",
        "address_2": "#",
        "city": "ALBUQUERQUE",
        "state": "NM",
        "zip_code": "87104"
    },
    "contact_info": {
        "email": "[email protected]",
        "telephone": "88888888888",
        "telephone_type_id": 1
    },
    "employment_info": {
        "employer_name": "SELF",
        "employment_status_id": 7,
        "annual_income": 9000000000000000,
        "monthly_income": 750000000000,
        "employment_start_year": 0,
        "employment_start_month": 0
    }
}

In order to build the payload as indicated in the question you need another class:

public class UserRequest {

    @SerializedName("user")
    private User user;
}

and use it when you perform the request with Retrofit:

@POST("/users")
void registerUser(@Body UserRequest User, Callback<User> cb);

Upvotes: 6

Egor
Egor

Reputation: 40218

With GSON, there's no difference whether you make those classes top-level or inner, it will work just fine in both scenarios. Your code looks good and should work properly.

Upvotes: 0

Related Questions