I fecht a Json-String from my server:
{"erfolgreich":"true","id":"14"}
When I call
//result is the string above
msgServer = gson.fromJson(result, MsgSpielerErstellenSA.class);
The boolean is always false...
What am I doing wrong?
Here is my MsgSpielerErstellenSA
public class MsgSpielerErstellenSA {
private long id;
private boolean isErfolgreich;
public MsgSpielerErstellenSA(long id, boolean isErfolgreich) {
super();
this.id = id;
this.isErfolgreich = isErfolgreich;
}
public boolean isErfolgreich() {
return isErfolgreich;
}
public void setErfolgreich(boolean isErfolgreich) {
this.isErfolgreich = isErfolgreich;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
Because the correct name for the boolean field is erfolgreich, not isErfolgreich. Please use the following class:
public class MsgSpielerErstellenSA {
private long id;
private boolean erfolgreich;
public MsgSpielerErstellenSA(long id, boolean isErfolgreich) {
this.id = id;
this.erfolgreich = isErfolgreich;
}
public boolean isErfolgreich() {
return erfolgreich;
}
public void setErfolgreich(boolean isErfolgreich) {
this.erfolgreich = isErfolgreich;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
But if you don't want to rename this field, you can use #SerializedName("erfolgreich") annotation on it
The name of your key "erfolgreich" in json string should be same as your class data member "isErfolgreich" or you should use #SerializedName notation before define your member. if gson can not match between a class member and json keys, then use the default value for that member type. so you can use the nikis solution or you can use notation like this:
#SerializedName("erfolgreich")
private boolean isErfolgreich;
Related
hey guys im using android's gson library to parse my JSON. everything is working fine in that I have that selected goal array sometimes it is full of values and sometimes it is an empty array. when array is empty I want to push an object init when I try to add its index
selected_goal.add(INDEX,VALUE)
I want to push an object at the place of value please help me how can I do it…
this is my pojo:
public class SelectedGoal {
#SerializedName("id")
#Expose
private String id;
#SerializedName("goal_name")
#Expose
private String goalName;
#SerializedName("image")
#Expose
private String image;
#SerializedName("position")
#Expose
private String position;
#SerializedName("athlete_id")
#Expose
private String athleteId;
#SerializedName("coach_id")
#Expose
private String coachId;
#SerializedName("current_goal_id")
#Expose
private String currentGoalId;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getGoalName() {
return goalName;
}
public void setGoalName(String goalName) {
this.goalName = goalName;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String getAthleteId() {
return athleteId;
}
public void setAthleteId(String athleteId) {
this.athleteId = athleteId;
}
public String getCoachId() {
return coachId;
}
public void setCoachId(String coachId) {
this.coachId = coachId;
}
public String getCurrentGoalId() {
return currentGoalId;
}
public void setCurrentGoalId(String currentGoalId) {
this.currentGoalId = currentGoalId;
}
}
selected_goal.add(INDEX,new SelectedGoal());
EDIT- To set values, make constuctor in your SelectedGoal class like.
public SelectedGoal(String id, String goalName, String image, String
position, String athleteId, String coachId, String currentGoalId) {
this.id = id;
this.goalName = goalName;
this.image = image;
this.position = position;
this.athleteId = athleteId;
this.coachId = coachId;
this.currentGoalId = currentGoalId;
}
finally add your object into list like..
selected_goal.add(INDEX,new SelectedGoal(id,goalName,image,position,athleteId,coachId,currentGoalId));
I am trying to implement Room database in my apps. And I created a simple model class called "Word".
#Entity(tableName = "word_table")
public class Word {
#PrimaryKey(autoGenerate = true)
private int id;
#NonNull
private String word;
public Word(#NonNull String word) {
this.word = word;
}
public int getId() {
return id;
}
public String getWord(){
return this.word;
}
}
But when I tried to build the apps. It says:
error: Cannot find setter for field.
private int id;
So I tried to add a setter for the "id" myself like:
public void setId(int id) {
this.id = id;
}
The full class looks like this:
#Entity(tableName = "word_table")
public class Word {
#PrimaryKey(autoGenerate = true)
private int id;
#NonNull
private String word;
public Word(#NonNull String word) {
this.word = word;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getWord(){
return this.word;
}
}
But it doesn't resolve the problem. So what is the proper way to use an auto-generated id in room?
In My application I get data from a web service and display those data in recycler view. After that I'm planing to add those data in to local sqlite database and display those data when user open application without internet connection.
Here's a simple model class I'm using to pars JSON result using GSON
public class Repo implements Parcelable {
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("name")
#Expose
private String name;
#SerializedName("url")
#Expose
private String url;
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(this.id);
dest.writeString(this.name);
dest.writeString(this.url);
}
public Repo() {
}
protected Repo(Parcel in) {
this.id = (Integer) in.readValue(Integer.class.getClassLoader());
this.name = in.readString();
this.url = in.readString();
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public String getUrl() {
return url;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setUrl(String url) {
this.url = url;
}
public static final Creator<Repo> CREATOR = new Creator<Repo>() {
#Override
public Repo createFromParcel(Parcel source) {
return new Repo(source);
}
#Override
public Repo[] newArray(int size) {
return new Repo[size];
}
};
}
I can create a almost identical model class to represent SQLite data. In here I'm using ORMlite. But this is very similar situation for other ORMs.
#DatabaseTable(tableName = Repo.TABLE_NAME)
public class Repo {
public static final String TABLE_NAME = "repo";
#DatabaseField(columnName = "repo_id")
private long repoId;
#DatabaseField(columnName = "name")
private String name;
public long getRepoId() {
return repoId;
}
public String getName() {
return name;
}
public void setRepoId(long repoId) {
this.repoId = repoId;
}
public void setName(String name) {
this.name = name;
}
}
But by the time I'm trying to save these data in to SQLite database I already have data objects set in GSON model classes. It's kind a redundant thing copy object from GSON model and setting that values in to SQLite model. So I came up with below solution by trying to use single model class to represent both.
#DatabaseTable(tableName = Repo.TABLE_NAME)
public class Repo implements Parcelable {
public static final String TABLE_NAME = "repo";
#DatabaseField(columnName = "repo_id")
#SerializedName("id")
#Expose
private Integer id;
#DatabaseField(columnName = "name")
#SerializedName("name")
#Expose
private String name;
#SerializedName("url")
#Expose
private String url;
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(this.id);
dest.writeString(this.name);
dest.writeString(this.url);
}
public Repo() {
}
protected Repo(Parcel in) {
this.id = (Integer) in.readValue(Integer.class.getClassLoader());
this.name = in.readString();
this.url = in.readString();
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public String getUrl() {
return url;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setUrl(String url) {
this.url = url;
}
public static final Creator<Repo> CREATOR = new Creator<Repo>() {
#Override
public Repo createFromParcel(Parcel source) {
return new Repo(source);
}
#Override
public Repo[] newArray(int size) {
return new Repo[size];
}
};
}
I have try this with different type of model class where it only had String type fields. Since GSON uses types like Integer,Boolean That stopping me from using same model for SQLite because database does not identify Integer as a type, in order to work it need to be int.
So what is the professional way to handle this ? Don't I have any other option other than going back to the method of creating two separate model class to represent SQLite and GSON.
Yout approach is absolutely correct, but i think you are putting too much effort reinventing the wheel
You can easily achieve the described task using Room
I'm trying to list objects that have an inner Collection. I can save and retrieve objects just fine, but when I do:
parentRepo.findAll()
Only the last object has it's child object listed, others has an empty collection.
Parent model
#ForeignCollectionField(eager = false)
private Collection<Child> childs;
Child model
#DatabaseField(foreign=true,foreignAutoRefresh=true)
private Parent parent;
eager true or false doesn't make any difference. If i query a child and get its parent, I can get it's children as well. What am I missing?
Edit:
It's working for the modeling that I made. My mistake was that I need a Many-to-many relation between parent and child. I made a quick research and what I need is an intermediate model to achieve this. I'll close this question and will try to made this many-to-many relation between my models.
I solve my Many-to-Many relationships like this:
This is an example from an ongoing project. I have a Many-to-Many relationship between Preparation and GlideWax. To solve it I use thee classes: Preparation, GlideWax and PreparationGlideWax. PreparationGlideWax represents the connections between the the other classes, just like the way you usually solve many-to-many relationships with a table that is a "link" between the tables in the relationship. As you can see GripWax and Structure also has a Many-to_many relationship to preparation. Here is the code:
GlideWax.java
#DatabaseTable(tableName = "glide_waxes")
public class GlideWax {
#DatabaseField(id = true)
private int id;
#DatabaseField(canBeNull = false)
private String name;
#DatabaseField
private String description;
#DatabaseField(canBeNull = false)
private int inUse;
#DatabaseField(foreign=true)
private WaxBrand waxBrand;
#DatabaseField(foreign=true)
private GlideWaxType glideWaxType;
#ForeignCollectionField
private ForeignCollection<PreparationGlideWax> preparationGlideWaxes;
#ForeignCollectionField
private ForeignCollection<TestSessionGlideWax> testSessionGlideWaxes;
public GlideWax() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getInUse() {
return inUse;
}
public void setInUse(int inUse) {
this.inUse = inUse;
}
public WaxBrand getWaxBrand() {
return waxBrand;
}
public void setWaxBrand(WaxBrand waxBrand) {
this.waxBrand = waxBrand;
}
public GlideWaxType getGlideWaxType() {
return glideWaxType;
}
public void setGlideWaxType(GlideWaxType glideWaxType) {
this.glideWaxType = glideWaxType;
}
public ForeignCollection<PreparationGlideWax> getPreparationGlideWaxes() {
return preparationGlideWaxes;
}
public void setPreparationGlideWaxes(ForeignCollection<PreparationGlideWax> preparationGlideWaxes) {
this.preparationGlideWaxes = preparationGlideWaxes;
}
public ForeignCollection<TestSessionGlideWax> getTestSessionGlideWaxes() {
return testSessionGlideWaxes;
}
public void setTestSessionGlideWaxes(ForeignCollection<TestSessionGlideWax> testSessionGlideWaxes) {
this.testSessionGlideWaxes = testSessionGlideWaxes;
}
}
Preparation.java
#DatabaseTable(tableName = "preparations")
public class Preparation {
#DatabaseField(generatedId=true)
private int id;
#ForeignCollectionField
private ForeignCollection<PreparationGlideWax> preparationGlideWaxes;
#ForeignCollectionField
private ForeignCollection<PreparationGripWax> preparationGripWaxes;
#ForeignCollectionField
private ForeignCollection<PreparationStructure> preparationStructures;
#DatabaseField(foreign=true, canBeNull = false)
private SkiPair skiPair;
#DatabaseField(foreign=true, canBeNull = false)
private SkiTester skiTester;
#DatabaseField(foreign=true)
private Rfid rfid;
#DatabaseField(foreign=true, canBeNull = false)
private TestSession testSession;
#ForeignCollectionField
private ForeignCollection<Measurement> measurements;
public Preparation() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public ForeignCollection<PreparationGlideWax> getPreparationGlideWaxes() {
return preparationGlideWaxes;
}
public void setPreparationGlideWaxes(ForeignCollection<PreparationGlideWax> preparationGlideWaxes) {
this.preparationGlideWaxes = preparationGlideWaxes;
}
public ForeignCollection<PreparationGripWax> getPreparationGripWaxes() {
return preparationGripWaxes;
}
public void setPreparationGripWaxes(ForeignCollection<PreparationGripWax> preparationGripWaxes) {
this.preparationGripWaxes = preparationGripWaxes;
}
public ForeignCollection<PreparationStructure> getPreparationStructures() {
return preparationStructures;
}
public void setPreparationStructures(ForeignCollection<PreparationStructure> preparationStructures) {
this.preparationStructures = preparationStructures;
}
public SkiPair getSkiPair() {
return skiPair;
}
public void setSkiPair(SkiPair skiPair) {
this.skiPair = skiPair;
}
public SkiTester getSkiTester() {
return skiTester;
}
public void setSkiTester(SkiTester skiTester) {
this.skiTester = skiTester;
}
public Rfid getRfid() {
return rfid;
}
public void setRfid(Rfid rfid) {
this.rfid = rfid;
}
public TestSession getTestSession() {
return testSession;
}
public void setTestSession(TestSession testSession) {
this.testSession = testSession;
}
}
PreparationGlideWax.java
#DatabaseTable(tableName = "preparation_glide_wax")
public class PreparationGlideWax {
#DatabaseField(generatedId=true)
private int id;
#DatabaseField(canBeNull = false)
private int layer;
#DatabaseField(foreign=true, canBeNull = false)
private GlideWax glideWax;
#DatabaseField(foreign=true, canBeNull = false)
private Preparation preparation;
public PreparationGlideWax() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getLayer() {
return layer;
}
public void setLayer(int layer) {
this.layer = layer;
}
public GlideWax getGlideWax() {
return glideWax;
}
public void setGlideWax(GlideWax glideWax) {
this.glideWax = glideWax;
}
public Preparation getPreparation() {
return preparation;
}
public void setPreparation(Preparation preparation) {
this.preparation = preparation;
}
}
As I said in the edit, I'm able to load the child from parent just fine. My problem is that I need a many-to-many relation between my models. I'll accept this answer in two days.
I have this FacebookUser object i've created:
private String id,name, status;
private double distance = -1, longitude, latitude;
private transient Drawable profilePicture,gender;
private boolean isFacebookFriend = false, isApplicationFriend = false, isClicked = false, isOnline = false, isLoaderChecked=false;
public FacebookUser()
{
}
public FacebookUser(String id, String name) {
this.setId(id);
this.setName(name);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
if (isApplicationFriend())
return name;
else
return "";
}
public void setName(String name) {
this.name = name;
}
public boolean isApplicationFriend() {
return isApplicationFriend;
}
public void setApplicationFriend(boolean isApplicationFriend) {
this.isApplicationFriend = isApplicationFriend;
}
}
My problem is with the getName() method.
When showing an ArrayList of FacebookUser in a ListView, i'm using FacebookUser.getName() to display the name in a TextView.
Now, i'm 100% sure that isApplicationFriend() return true - and i've even logged it near the TextView text insert.
So why does getName() keep returning "" ??
You're right you're right... i'm so silly...
The method was called before and emptied the name property...
Sorry...