I want to convert the following JSON result to Pojos in Retrofit:
{
"status": "success",
"body": {
"token": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQi",
"profile": [
{
"id": 18,
"first_name": "",
"last_name": "",
"mobile": "0000000",
"email": null,
"code_melli": null,
"image": "http://example.com/image.jpg",
"address": null,
"credit": 0,
"status": "active",
"gender": "male",
"role": "customer",
"created_at": "2020-02-02"
}
]
}
}
My model classes are as follow:
public class Customer {
@SerializedName("status")
private String status;
@SerializedName("body")
private Body body;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Body getBody() {
return body;
}
public void setBody(Body body) {
this.body = body;
}
}
and the Body,
public class Body {
@SerializedName("token")
private String token;
@SerializedName("profile")
private List<Profile> profile = null;
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public List<Profile> getProfile() {
return profile;
}
public void setProfile(List<Profile> profile) {
this.profile = profile;
}
}
and Profile is,
public class Profile {
@SerializedName("id")
@Nullable
private Integer id;
@SerializedName("first_name")
@Nullable
private String firstName;
@SerializedName("last_name")
@Nullable
private String lastName;
@SerializedName("mobile")
@Nullable
private String mobile;
@SerializedName("email")
@Nullable
private Object email;
@SerializedName("code_melli")
@Nullable
private Object codeMelli;
@SerializedName("image")
@Nullable
private String image;
@SerializedName("address")
@Nullable
private Object address;
@SerializedName("credit")
@Nullable
private Integer credit;
@SerializedName("status")
@Nullable
private String status;
@SerializedName("gender")
@Nullable
private String gender;
@SerializedName("role")
@Nullable
private String role;
@SerializedName("created_at")
@Nullable
private String createdAt;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public Object getEmail() {
return email;
}
public void setEmail(Object email) {
this.email = email;
}
public Object getCodeMelli() {
return codeMelli;
}
public void setCodeMelli(Object codeMelli) {
this.codeMelli = codeMelli;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public Object getAddress() {
return address;
}
public void setAddress(Object address) {
this.address = address;
}
public Integer getCredit() {
return credit;
}
public void setCredit(Integer credit) {
this.credit = credit;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getCreatedAt() {
return createdAt;
}
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
But the problem is that in the response I can get the value of status and check it to be equal to success however any method on profile give null pointer exception, the code for accessing profile fields is as follow,
Customer customer = response.body();
customer.getBody().getProfile().get(0).getFirstName();
I use the following gson for the Retrofit and everythings works
Gson gson = new GsonBuilder()
.serializeNulls()
.setLenient()
.create();