如何在改造中发送对象列表

时间:2019-09-18 09:36:30

标签: android

我需要在翻新时发送以下对象的列表,但始终会收到错误消息,您的身体为空

{
  "height": "20",
  "weight": "40",
  "unit": "kg"
}
GetDataService service = RetrofitClientInstance.getRetrofitInstance().create(GetDataService.class);
        Call<ResponseBody> call = service.getAllPhotos("");
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Log.v("SUCCESS", response.body() + " ");
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Toast.makeText(MainActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
            }
        });

1 个答案:

答案 0 :(得分:0)

首先,您必须使用click here

创建模型

您的模型将是这样

public class User {

@SerializedName("height")
@Expose
private String height;
@SerializedName("weight")
@Expose
private String weight;
@SerializedName("unit")
@Expose
private String unit;

public String getHeight() {
return height;
}

public void setHeight(String height) {
this.height = height;
}

public String getWeight() {
return weight;
}

public void setWeight(String weight) {
this.weight = weight;
}

public String getUnit() {
return unit;
}

public void setUnit(String unit) {
this.unit = unit;
}

}

在您的API接口上创建此方法

 @POST("/service name for example register")
    Call<ResponseBody> postUsers(@Body List<User> userRequest);

在主要活动中,使用以下代码

 public void postUsers(List<Users> userRequest) {
            GetDataService service = RetrofitClientInstance.getRetrofitInstance().create(GetDataService.class);
            Call<ResponseBody> call = service.postUsers(userRequest);
            call.enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    Log.v("SUCCESS", response.body() + " ");
                }

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {
                    Toast.makeText(MainActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
                }
            });
        }