发送JSON ARRAY改装2(ANDROID)

时间:2017-03-30 09:19:48

标签: android json retrofit2

我是新手,知道这个问题有很多帖子,但我找不到答案。

因此,我需要使用retrofit2和Request“PATCH”升级用户及其议程。但是,我不知道如何使用Retrofit2。

即使我的研究......没什么可行的!

你能帮帮我吗?

预付款:D

以下是我要发送的内容:

{
    "gender": "M",
    "trainerName": "Patrick",
    "laterality": "L",
    "email": "xxxx@gmail.com",
    "phoneNumber": "XXXXX",
    "agendaWeekDays": [
        {
            "position": 1,
            "startSeconds": 648000,
            "endSeconds": 72000
        }, 
        {
            "position": 2,
            "startSeconds": 0,
            "endSeconds": 459
        }
    ]
}

回复是Just:

{
  "success"
}

我的要求

public interface Minterface {
@FormUrlEncoded
@PATCH("api/members/current")
Call<String> test(
        @Field("access_token") String access_token,
        @Field("agendaWeekDays") ArrayList<Agenda> agendaWeekDays);
}

Agenda.class

public class Agenda 
{

    @SerializedName("position")
    @Expose
    private Integer position;
    @SerializedName("startSeconds")
    @Expose
    private Integer startSeconds;
    @SerializedName("endSeconds")
    @Expose
    private Integer endSeconds;

    public Integer getPosition() {
        return position;
    }

    public void setPosition(Integer position) {
        this.position = position;
    }

    public Integer getStartSeconds() {
        return startSeconds;
    }

    public void setStartSeconds(Integer startSeconds) {
        this.startSeconds = startSeconds;
    }

    public Integer getEndSeconds() {
        return endSeconds;
    }

    public void setEndSeconds(Integer endSeconds) {
        this.endSeconds = endSeconds;
    }
}

1 个答案:

答案 0 :(得分:2)

public class Example {

@SerializedName("gender")
@Expose
private String gender;
@SerializedName("trainerName")
@Expose
private String trainerName;
@SerializedName("laterality")
@Expose
private String laterality;
@SerializedName("email")
@Expose
private String email;
@SerializedName("phoneNumber")
@Expose
private String phoneNumber;
@SerializedName("agendaWeekDays")
@Expose
private List<AgendaWeekDay> agendaWeekDays = null;

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}

public String getTrainerName() {
return trainerName;
}

public void setTrainerName(String trainerName) {
this.trainerName = trainerName;
}

public String getLaterality() {
return laterality;
}

public void setLaterality(String laterality) {
this.laterality = laterality;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPhoneNumber() {
return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}

public List<AgendaWeekDay> getAgendaWeekDays() {
return agendaWeekDays;
}

public void setAgendaWeekDays(List<AgendaWeekDay> agendaWeekDays) {
this.agendaWeekDays = agendaWeekDays;
}

}

public class AgendaWeekDay {

@SerializedName("position")
@Expose
private Integer position;
@SerializedName("startSeconds")
@Expose
private Integer startSeconds;
@SerializedName("endSeconds")
@Expose
private Integer endSeconds;

public Integer getPosition() {
return position;
}

public void setPosition(Integer position) {
this.position = position;
}

public Integer getStartSeconds() {
return startSeconds;
}

public void setStartSeconds(Integer startSeconds) {
this.startSeconds = startSeconds;
}

public Integer getEndSeconds() {
return endSeconds;
}

public void setEndSeconds(Integer endSeconds) {
this.endSeconds = endSeconds;
}

}

之后你想通过改造创建对象o示例类

发送它
Example example = new Example();

之后将此对象的值设置为:

example.setGender("M");
.
.
.
example.setAgendaWeekDays(your arraylist);

然后

retrofit.method(token,example).enqueue(new Callback<Void>()
{
@Override
public void onResponse(Call<Void> call, Response<Void> response)
{
                                            if(response.message().equalsIgnoreCase("OK"))
{
}
else 
{}

然后将您的方法定义为:

@POST("link here")
    Call<Void> method(@Header("Authorization") String authorization, @Body Example example);

它会起作用。