Karan Mehta
Karan Mehta

Reputation: 1541

How to send json object inside another json object using Retrofit in android?

I want to send json object inside another json object in post api call with retrofit 2 in android

I want to send data like this :

    {
        "firstName":"abc",
        "emailId":"[email protected]",
        "userType":{
            "id":"1"
        },
        "floor":{
            "id":"2"
        }
    }

I have created 3 model classes like this :

1) Employee :

public class Employee implements Serializable {

    @SerializedName("firstName")
    @Expose
    private String firstName;

    @SerializedName("emailId")
    @Expose
    private String emailId;

    @SerializedName("userType")
    @Expose
    private Type userType;

    @SerializedName("floor")
    @Expose
    private Floor floor;

    public Employee(String firstName, String emailId, Type userType, Floor floor) {
        this.firstName = firstName;
        this.emailId = emailId;
        this.userType = userType;
        this.floor = floor;
    }

    public Employee(String firstName, String emailId, Type userType) {
        this.firstName = firstName;
        this.emailId = emailId;
        this.userType = userType;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getEmailId() {
        return emailId;
    }

    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }

    public Type getUserType() {
        return userType;
    }

    public void setUserType(Type userType) {
        this.userType = userType;
    }

    public Floor getFloor() {
        return floor;
    }

    public void setFloor(Floor floor) {
        this.floor = floor;
    }
}

2) Type :

public class Type implements Serializable {

    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("userType")
    @Expose
    private String userType;

    public Type(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserType() {
        return userType;
    }

    public void setUserType(String userType) {
        this.userType = userType;
    }
}

3) Floor :

public class Floor implements Serializable {

    @SerializedName("id")
    @Expose
    private String id;

    public Floor(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

And I've created API service like this :

public interface GetDataService {
    @POST("employees")
    Call<Employee> registerUser(@Body Employee employee);
}

But it gives me bad request error (Code 400), so how to solve it?

Upvotes: 2

Views: 631

Answers (2)

Sujan Poudel
Sujan Poudel

Reputation: 853

I think your model classes must be like this

Employee.java

public class Employee implements Serializable
{

@SerializedName("firstName")
@Expose
private String firstName;
@SerializedName("emailId")
@Expose
private String emailId;
@SerializedName("userType")
@Expose
private UserType userType;
@SerializedName("floor")
@Expose
private Floor floor;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getEmailId() {
return emailId;
}

public void setEmailId(String emailId) {
this.emailId = emailId;
}

public UserType getUserType() {
return userType;
}

public void setUserType(UserType userType) {
this.userType = userType;
}

public Floor getFloor() {
return floor;
}

public void setFloor(Floor floor) {
this.floor = floor;
}

}

----------------------Floor.java-------------------------

public class Floor implements Serializable
{

@SerializedName("id")
@Expose
private String id;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

}

------------------UserType.java-------------------------

public class UserType implements Serializable
{

@SerializedName("id")
@Expose
private String id;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

}

It is self explanatory I think

Upvotes: 3

Avinash tiwari
Avinash tiwari

Reputation: 269

Please use this instead of your Type class

  public class Type implements Serializable {

   @SerializedName("id")
   @Expose
   private Integer id;

   public Type(Integer id) {
        this.id = id;
    }

   public Integer getId() {
        return id;
   }

   public void setId(Integer id) {
        this.id = id;
    }


}

Upvotes: 0

Related Questions