如何在 android 中使用 Retrofit 将 json 对象发送到另一个 json 对象中?

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

我想在 post 中的另一个 json 对象中发送 json 对象 api android

中的改进 2 调用

我想像这样发送数据:

    {
        "firstName":"abc",
        "emailId":"abc@a.a",
        "userType":{
            "id":"1"
        },
        "floor":{
            "id":"2"
        }
    }

我创建了 3 个这样的模型 类:

1) 员工:

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) 类型:

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) 楼层 :

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

我创建了 API 服务,如下所示:

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

但是报错bad request error (Code 400),怎么解决?

请使用这个代替您的 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;
    }


}

我觉得你的模型类一定是这样的

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

}

我认为这是不言自明的