Reputation: 198
Can someone please explain what i am doing wrong, below i am posting my code
My Pasta Class
public class Pasta {
String Name;
String Desc;
String Price;
String Temp;
public Pasta() {
}
public Pasta(String name, String desc, String price, String temp) {
Name = name;
Desc = desc;
Price = price;
Temp = temp;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getDesc() {
return Desc;
}
public void setDesc(String desc) {
Desc = desc;
}
public String getPrice() {
return Price;
}
public void setPrice(String price) {
Price = price;
}
public String getTemp() {
return Temp;
}
public void setTemp(String temp) {
Temp = temp;
}
My Java Code
Ref.child("Products").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Pasta List
for (DataSnapshot pasta : dataSnapshot.child("Pasta").getChildren()) {
Pasta p = pasta.getValue(Pasta.class);
ProductsFragment.pastaArrayList.add(p);//My Pasta ArrayList
Log.e("Pasta Object: ",p.getName());
}
Exception
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.pizzanpasta, PID: 5177
com.firebase.client.FirebaseException: Failed to bounce to type
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:183)
at com.pizzanpasta.Handlers.FirebaseHandler$1.onDataChange(FirebaseHandler.java:74)
at com.firebase.client.Query$1.onDataChange(Query.java:158)
at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45)
at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5300)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:830)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:646)
at dalvik.system.NativeStart.main(Native Method)
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Name" (class com.pizzanpasta.Models.Pasta), not marked as ignorable (4 known properties: , "desc", "price", "name", "temp"])
at [Source: java.io.StringReader@42e16a68; line: 1, column: 10] (through reference chain: com.pizzanpasta.Models.Pasta["Name"])
at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:555)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:708)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1160)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:315)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:181)
at com.pizzanpasta.Handlers.FirebaseHandler$1.onDataChange(FirebaseHandler.java:74)
at com.firebase.client.Query$1.onDataChange(Query.java:158)
at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45)
at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5300)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:830)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:646)
at dalvik.system.NativeStart.main(Native Method)
If i dont cast snapsnot to class
for (DataSnapshot pasta : dataSnapshot.child("Pasta").getChildren()) {
Log.e("Pasta Node; ", pasta.toString());
}
That is Log Result:
05-14 15:18:13.865 10913-10913/com.pizzanpasta E/Pasta Node;: DataSnapshot { key = Bolognese (Beef), value = {Name=Bolognese (Beef), Temp=temp, Desc=Bolognese Pasta with Red sause, Price=400} }
05-14 15:18:13.866 10913-10913/com.pizzanpasta E/Pasta Node;: DataSnapshot { key = Chicken La'Primavera, value = {Name=Chicken La'Primavera, Temp=temp, Desc=Backed Chicken La'Primavera Pasta, Price=400} }
05-14 15:18:13.866 10913-10913/com.pizzanpasta E/Pasta Node;: DataSnapshot { key = Lasagne, value = {Name=Lasagne, Temp=temp, Desc=Backed Lasagne Pasta, Price=300} }
05-14 15:18:13.866 10913-10913/com.pizzanpasta E/Pasta Node;: DataSnapshot { key = Marinara, value = {Name=Marinara, Temp=temp, Desc=Marinara Pasta with Red sause, Price=400} }
In Log result you can see it is like Pasta Class but i am unable to cast it to Pasta Class Object
Upvotes: 3
Views: 2456
Reputation: 363825
Since your setter method is named setName(…)
Jackson assumes the variable is named name
because of the Java naming conventions.
It is the reason of your issue.
The best way to resolve the issue should be to use "desc", "price", "name", "temp" inside your Firebase database.
Otherwise you can use the @JsonProperty annotation, like so:
@JsonProperty("Name")
public void setName(String n) {
// ...
}
Upvotes: 4
Reputation: 598807
@Gabriele's answer will work. Two alternatives:
In your current setup, Firebase cannot map the JSON properties to the Java object, because there is a mismatch in the casing it expects. When you have a method getName()
, the expected property in JSON is name
. Your JSON uses Name
.
To use your current code, you can modify the JSON to:
{
name: "Bolognese (Beef)",
temp: "temp",
desc: "Bolognese Pasta with Red sause",
price: 400
}
With these property names, Firebase will be able to find the correct fields/setters in your Java class.
You can also declare the fields as public and go without getters/setters:
public class Pasta {
public String Name;
public String Desc;
public String Price;
public String Temp;
public Pasta() {
}
public Pasta(String name, String desc, String price, String temp) {
Name = name;
Desc = desc;
Price = price;
Temp = temp;
}
}
Now Firebase will find public fields with the same name as the JSON properties, so it will be able to correctly map them.
Upvotes: 1