简体   繁体   中英

Android Retrofit always returns false value for the type boolean while fetching jsonobject

I am new using retrofit and here i am stuck with an problem where retrofit always returns false for boolean type object which is actually 1(true) which i am retriving.

here below is my pojo class for retriving data

public class JobsModel implements Serializable {

@SerializedName("posted_on")
private String postedOn;
@SerializedName("is_active")
private boolean isActive=false;

public JobsModel(String postedOn, boolean isActive) {
    this.postedOn=postedOn;
    this.isActive=isActive;
}

public Boolean getPostedOn() {
    return postedOn
}

public void setPostedOn(String postedOn) {
    postedOn= postedOn
}

public Boolean getActive() {
    return isActive;
}

public void setActive(Boolean active) {
    isActive = active;
}

}

any help would be much appreciated

First you have to be sure that @SerializedName("is_active") is correct and matching your json name.

Also you do not need to init value like:

private boolean isActive=false;

I modified your getter and setter. Can you try with this:

public boolean getActive() {
    return isActive;
}

public void setActive(boolean active) {
    isActive = active;
}

If you're using Retrofit 2.0 there's a strange behavior (i think it's a bug) about parsing boolean .

When we try to parse boolean json object which's name starts with "I" it always returns false. This is not relevant to you but maybe there's a problem like this. If you can, you can try to use a different json name for your object.

Boolean is primitive type in Java an it's default value is false If your json is_active value hasn't any value then is_active by default be false! For avoiding this problem use Boolean not boolean type!

first of all change private boolean isActive=false; to private boolean Active;

Secondly change

   public void setActive(Boolean active) {
        isActive = active;   }

to

public void setActive(boolean active) {
    Active = active;

}

and If your JSON contains any object starting with I then change it,cause it will always return false. Hope this will be helpful to you.

You already initialised this as false. Do this

private boolean isActive;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM