2

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":"abc@a.a",
        "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?

2
  • can you provide the actual stack-trace? Dec 9, 2019 at 4:49
  • I have checked from debugging, it returns code 400 in response when i call that api Dec 9, 2019 at 4:52

2 Answers 2

3

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

4
  • As i said earlier i need to have userType field in Type class Dec 9, 2019 at 5:19
  • this model class generates the same json as you posted above, the above json is what you need right? Dec 9, 2019 at 5:23
  • Now its done even without removing userType field from class Dec 9, 2019 at 5:39
  • Nice to hear that Dec 9, 2019 at 5:42
0

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;
    }


}
2
  • But i need to have userType also in that class, although its not neccessary Dec 9, 2019 at 4:53
  • Did you called the API on Postman ? if you have then please share the screenshot of request in postman Dec 9, 2019 at 5:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.