0

I have a problem when I want to retrieve an array from a post response

this is my post

{
    "user_email": "[email protected]",
    "user_password": "12345"
}

and this is my post response , I want get the token value

{
    "status": true,
    "code": 200,
    "message": "Request Succeded: Login success",
    "data": [
        {
            "token": "bsWIVXTLuud2ZbdnUvI8037fT7D0t7MTvusBrNjskah"
        }
    ]
}

this is my model LoginModel.java

@SerializedName("data")
@Expose
private Data data ;

public Data getData() {
    return data;
}

public void setData (Data data) {
    this.data = data;
}

public LoginModel(String user_email, String user_password) {
    this.user_email = user_email;
    this.user_password = user_password;
}

Data.java

public String getToken() {
    return token;
}

public void setToken(String token) {
   this.token = token;
}

there code I'm tried before ,the text view show nothing

LoginModel loginModelresponse = response.body();

String token = loginModelresponse.getData().getToken();

textView.setText(token);

I want get the token from my post response.

3 Answers 3

1

Your data value is an array of tokens, not an object itself

Create a Token class with a token String field, then replace the Data class like so

@SerializedName("data")
@Expose
private List<Token> data ;

To get the token, you must iterate the list

Sign up to request clarification or add additional context in comments.

1 Comment

how to iterate the list ?
0

Try to change your LoginModel class, because your response data object is Array type

public class LoginModel {
@SerializedName("status")
@Expose
private Boolean status;
@SerializedName("code")
@Expose
private Integer code;
@SerializedName("message")
@Expose
private String message;
@SerializedName("data")
@Expose
private List<Data> data = null;

public Boolean getStatus() {
return status;
}

public void setStatus(Boolean status) {
this.status = status;
}

public Integer getCode() {
return code;
}

 public void setCode(Integer code) {
 this.code = code;
 }

 public String getMessage() {
return message;
 }

public void setMessage(String message) {
this.message = message;
}

public List<Data> getData() {
return data;
}

 public void setData(List<Data> data) {
 this.data = data;
}

}

Comments

0

use this site to generate a correct response class in java

 public class Tokens implements Serializable
    {

    @SerializedName("token")
    @Expose
    private String token;
    private final static long serialVersionUID = 1577013820593763604L;

    public String getToken() {
    return token;
    }

    public void setToken(String token) {
    this.token = token;
    }

    }
  //  -----------------------------------com.example.Response.java-----------

    package com.example;

    import java.io.Serializable;
    import java.util.List;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

    public class Response implements Serializable
    {

    @SerializedName("status")
    @Expose
    private boolean status;
    @SerializedName("code")
    @Expose
    private long code;
    @SerializedName("message")
    @Expose
    private String message;
    @SerializedName("data")
    @Expose
    private List<Tokens> data = null;
    private final static long serialVersionUID = -3002290394951662690L;

    public boolean isStatus() {
    return status;
    }

    public void setStatus(boolean status) {
    this.status = status;
    }

    public long getCode() {
    return code;
    }

    public void setCode(long code) {
    this.code = code;
    }

    public String getMessage() {
    return message;
    }

    public void setMessage(String message) {
    this.message = message;
    }

    public List<Tokens> getData() {
    return data;
    }

    public void setData(List<Tokens> data) {
    this.data = data;
    }

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.