SEND JSON ARRAY RETROFIT 2 (ANDROID)

安稳与你 提交于 2021-02-18 14:29:46

问题


I'm novice and know there are many post for this question but i don't find my answer.

So, I need to upgrade a user and his agenda with retrofit2 and the Request "PATCH". But, i don't know how do that with Retrofit2.

Even with my research ... Nothing work !

Can you help me please ?

Thx for advance :D

Here is what I have to send :

{
    "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
        }
    ]
}

The response is Just :

{
  "success"
}

my REQUEST

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:


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

}

After that where you want to send it via retrofit create object o example class

Example example = new Example();

After that set values to this object as :

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

then

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

Then define your method as:

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

It will work.



来源:https://stackoverflow.com/questions/43113529/send-json-array-retrofit-2-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!