将Firebase数据导入android类

时间:2015-12-11 21:45:22

标签: android firebase firebase-realtime-database

我正在尝试查询Firebase数据并将其存储到下面的示例链接中的恐龙类中。将Firebase直接导入Dinosaur Facts类时出现问题。它能够创建Query并输入“onChildAdded”方法。然后它进入恐龙构造函数类,但在此之后崩溃。

DinosaurFacts facts = snapshot.getValue(DinosaurFacts.class); //CRASHES HERE

https://www.firebase.com/docs/android/guide/retrieving-data.html

这是DinosaurFacts类

package jobsme.com.firebasequery;

import android.util.Log;

public class DinosaurFacts {
    long height;
    double length;
    long weight;

    public DinosaurFacts() {
        Log.i("MyActivity", "FIIIIIIIIIIIIIIIIIINDMEEEE2");
        // empty default constructor, necessary for Firebase to be able to deserialize blog posts
    }

    public long getHeight() {
        Log.i("MyActivity", "FIIIIIIIIIIIIIIIIIINDMEEEE3");
        return height;
    }

    public double getLength() {
        return length;
    }

    public long getWeight() {
        return weight;
    }

}

这是MainAcitivity中的onCreate方法

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Firebase.setAndroidContext(this);

    Firebase ref = new Firebase("https://dinosaur-facts.firebaseio.com/dinosaurs");
    Query queryRef = ref.orderByChild("height");

    queryRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot snapshot, String previousChild) {
            Log.i("MyActivity", "onChildAdded");
            DinosaurFacts facts = snapshot.getValue(DinosaurFacts.class);
            Log.i("MyActivity", snapshot.getKey() + " was " + facts.getHeight() + " meters tall");
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
        // ....
    });

    Log.i("MyActivity", "FIIIIIIIIIIIIIIIIIINDMEEEE");


}

查询成功但导入崩溃。希望这意味着我正确设置了一切?

这是日志输出:

12-11 16:48:37.793 5581-5581/jobsme.com.firebasequery E/AndroidRuntime: FATAL EXCEPTION: main
Process: jobsme.com.firebasequery, PID: 5581
com.firebase.client.FirebaseException: Failed to bounce to type
    at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:196)
    at jobsme.com.firebasequery.MainActivity$2.onChildAdded(MainActivity.java:45)
    at com.firebase.client.core.ChildEventRegistration$1.run(ChildEventRegistration.java:50)
    at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:37)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5001)
    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:785)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "vanished" (class jobsme.com.firebasequery.DinosaurFacts), not marked as ignorable (3 known properties: , "weight", "length", "height"])
    at [Source: java.io.StringReader@52851428; line: 1, column: 33] (through reference chain: jobsme.com.firebasequery.DinosaurFacts["vanished"])
    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:192)
    at jobsme.com.firebasequery.MainActivity$2.onChildAdded(MainActivity.java:45) 
    at com.firebase.client.core.ChildEventRegistration$1.run(ChildEventRegistration.java:50) 
    at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:37) 
    at android.os.Handler.handleCallback(Handler.java:733) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:136) 
    at android.app.ActivityThread.main(ActivityThread.java:5001) 
    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:785) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
    at dalvik.system.NativeStart.main(Native Method)     

1 个答案:

答案 0 :(得分:3)

这是该数据库中恐龙之一的JSON:

"bruhathkayosaurus": {
  "appeared": -70000000,
  "height": 25,
  "length": 44,
  "order": "saurischia",
  "vanished": -70000000,
  "weight": 135000
},

您使用的DinosaurFacts课程只包含以下属性的字段+获取者:heightlengthweight。因此,当Firebase尝试将JSON反序列化为DinosaurFacts对象时,它会抱怨未映射的属性。

摆脱错误的最简单方法是告诉Jackson(Firebase在内部使用JSON和Java之间的映射)来忽略它在JSON中遇到的任何未映射的属性:

@JsonIgnoreProperties(ignoreUnknown = true)
public static class DinosaurFacts {

但是要小心这个。如果您现在将<{em} 一个DinosaurFacts对象重新写回数据库,它将只有heightlengthweight属性。它不会有:appearedordervanished。实际上:即使之前属性存在于数据库中,将该对象写回也将删除它们。

因此,正确的解决方案是将JSON结构中的所有属性映射到Java类:

public static class DinosaurFacts {
    long appeared, vanished;
    double height, length, weight;
    String order;

    public long getAppeared() {
        return appeared;
    }

    public long getVanished() {
        return vanished;
    }

    public double getHeight() {
        return height;
    }

    public double getLength() {
        return length;
    }

    public double getWeight() {
        return weight;
    }

    public String getOrder() {
        return order;
    }
}