忽略POJO中没有的东西

时间:2016-06-22 17:43:22

标签: android firebase firebase-realtime-database

我尝试使用Firebase向我的应用程序发出一些请求。我可以保存数据,但是当我尝试检索它们时,我收到此错误

  06-20 12:07:15.151 28158-28158/sgk.com.br.sgk E/AndroidRuntime: FATAL EXCEPTION: main
Process: sgk.com.br.sgk, PID: 28158
com.firebase.client.FirebaseException: Failed to bounce to type
    at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:183)
    at sgk.com.br.sgk.Activity.GreatTrainsActivity$1.onDataChange(GreatTrainsActivity.java:65)
    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:746)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5443)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
 Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "vdsrXOFXRZbAfmoEQ3WEVB1GDDa2" (class sgk.com.br.sgk.Others.Train), not marked as ignorable (2 known properties: , "description", "local"])
    at [Source: java.io.StringReader@ab8ad7; line: 1, column: 34] (through reference chain: sgk.com.br.sgk.Others.Train["vdsrXOFXRZbAfmoEQ3WEVB1GDDa2"])
    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 sgk.com.br.sgk.Activity.GreatTrainsActivity$1.onDataChange(GreatTrainsActivity.java:65) 
    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:746) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5443) 
    at java.lang.reflect.Method.invoke(Native Method)

我已经知道问题所在。

我的数据库就像那样

{
  "Train" : {
    "description" : "dbdjem",
    "local" : "shsusn"
  },
  "users" : {
    "f01dfa18-54ac-41ea-97ff-97d0e40b872b" : {
      "email" : "gui@email.com"
    },
    "vdsrXOFXRZbAfmoEQ3WEVB1GDDa2" : {
      "email" : "leo@email.com"
    }
  }
}

如果除了POJO中的内容之外我还删除了其他所有内容,它的效果非常好。

所以,解释一下,我的问题是:除了我的POJO上的内容之外,有没有办法忽略数据库中的其他所有内容?

修改

这是我的POJO

public class Train {

    private String local;
    private String description;
    public String data;

    public Train() {
    }

    public String getLocal() {
        return local;
    }

    public void setLocal(String local) {
        this.local = local;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Exclude
    public String getData(){return data;}
}

这里是电话

Firebase ref = new Firebase(Constants.FIREBASE_URL);
ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        for (DataSnapshot postSnapshot: snapshot.getChildren()) {
            //Getting the data from snapshot
            Train train = postSnapshot.getValue(Train.class);
            //Adding it to a string
            String string = "Local: " + train.getLocal() + "\nDescrição: " + train.getDescription() + "\n\n";
            //Displaying it on textview
            textViewPersons.setText(string);
        }
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {
        System.out.println("The read failed: " + firebaseError.getMessage());
    }
});

1 个答案:

答案 0 :(得分:4)

您将entities.spawnEntity[Bird] 附加到数据库的根目录。根下的孩子是:

Rabbit( /* params here * /)

这些都不适合您已创建的Java ChildEventListener课程。

解决方案是将侦听器附加到Train users 节点:

Train