Reputation: 21
My REST API response looks like this:
{
"message": "OK",
"data": {
"api_token": "1dwdafg45567fsf",
"name": "Albert",
"second_name": "Ferbs"
}
}
My Interface is:
@POST("api/login")
Call<LoginResponse> loginUser(@Body LoginRequest loginRequest);
I want to get a value from "api_token". My LoginResponse is:
public class LoginResponse implements Serializable {
private String api_token;
public String getApi_token() {
return api_token;
}
public void setApi_token(String api_token) {
this.api_token= api_token;
}
}
But loginResponse.getApi_token() returns me "null". What should i do?
Upvotes: 2
Views: 62
Reputation: 1278
you should do as below:
first create Data class parser
public class Data implements Serializable {
@SerializedName("api_token")
private String api_token;
public String getApi_token() {
return api_token;
}
}
then change your loginResponse class :
public class LoginResponse implements Serializable {
@SerializedName("message")
private String message;
@SerializedName("data")
private Data data;
public String getMessage() {
return message;
}
public String getData() {
return data;
}
}
then you can call your method
response.getData().getApi_token();
Upvotes: 2