Android FireBase检索数据

时间:2016-07-05 05:33:14

标签: android json firebase

我将一个json文件从parse导入到firebase,json文件有一个项目数组:

{ "results": [
    {
        "Bids": ".................",
        "Instrument": ".................",
        "InstrumentCompany": ".................",
        "InstrumentModel": ".................",
        "InstrumentUrl": "http://.................",
        "LastUpdatedPrice": "3,790.00",
        "TitleTag": ".................",
        "Type": ".................",
        "UrlForImg": "http://.................861680.jpg",
        "createdAt": "2015-12-01T09:42:23.994Z",
        "objectId": "0IT7.................",
        "updatedAt": "2016-02-04T11:01:37.159Z"
    },
    {
        "Bids": ".................",
        "Instrument": ".................",
        "InstrumentCompany": ".................",
        "InstrumentModel": ".................",
        "InstrumentUrl": "http://.................",
        "LastUpdatedPrice": ".................",
        "TitleTag": ".................",
        "Type": ".................",
        "UrlForImg": "http://.................jpeg",
        "createdAt": "2015-12-01T09:43:29.138Z",
        "objectId": "0QNyY.................",
        "updatedAt": "2016-02-04T11:04:03.561Z"
    }
] }

火基地的网址是: https://console.firebase.google.com/project/abcdefghijk/database/data/ClassicGuitars

我创建了一个用于检索对象的java类:

public class ClassicGuitar {
    private String Bids; 
    private String Instrument; 
    private String InstrumentCompany; 
    private String InstrumentModel; 
    private String InstrumentUrl; 
    private String LastUpdatedPrice; 
    private String TitleTag; 
    private String Type; 
    private String UrlForImg; 
    String createdAt; 
    String objectId; 
    String updatedAt; 

    public ClassicGuitar (){};
    public String getBids() {
        return Bids;
    }
    public void setBids(String bids) {
        Bids = bids;
    }
    public String getInstrument() {
        return Instrument;
    }
    public void setInstrument(String instrument) {
        Instrument = instrument;
    }
    public String getInstrumentCompany() {
        return InstrumentCompany;
    }
    public void setInstrumentCompany(String instrumentCompany) {
        InstrumentCompany = instrumentCompany;
    }
    public String getInstrumentModel() {
        return InstrumentModel;
    }
    public void setInstrumentModel(String instrumentModel) {
        InstrumentModel = instrumentModel;
    }
    public String getInstrumentUrl() {
        return InstrumentUrl;
    }
    public void setInstrumentUrl(String instrumentUrl) {
        InstrumentUrl = instrumentUrl;
    }
    public String getLastUpdatedPrice() {
        return LastUpdatedPrice;
    }
    public void setLastUpdatedPrice(String lastUpdatedPrice) {
        LastUpdatedPrice = lastUpdatedPrice;
    }
    public String getTitleTag() {
        return TitleTag;
    }
    public void setTitleTag(String titleTag) {
        TitleTag = titleTag;
    }
    public String getType() {
        return Type;
    }
    public void setType(String type) {
        Type = type;
    }
    public String getUrlForImg() {
        return UrlForImg;
    }
    public void setUrlForImg(String urlForImg) {
        UrlForImg = urlForImg;
    }
    public String getCreatedAt() {
        return createdAt;
    }
    public void setCreatedAt(String createdAt) {
        this.createdAt = createdAt;
    }
    public String getObjectId() {
        return objectId;
    }
    public void setObjectId(String objectId) {
        this.objectId = objectId;
    }
    public String getUpdatedAt() {
        return updatedAt;
    }
    public void setUpdatedAt(String updatedAt) {
        this.updatedAt = updatedAt;
    }


}

按照firebase指南的说明我这样做了:

    Firebase.setAndroidContext(this);
        Firebase ref = new Firebase("https://abcdefghijk.firebaseio.com/");

Firebase classicGuitarsRef = ref.child("ClassicGuitars").child("results");
            classicGuitarsRef.addValueEventListener(new ValueEventListener() {

                @Override
                public void onDataChange(DataSnapshot snapShot) {

                    Log.i("firebase","There are " + snapShot.getChildrenCount() + " classic guitars");
                    for (DataSnapshot classicSnapShot : snapShot.getChildren()) {
                        ClassicGuitar classic = classicSnapShot.getValue(ClassicGuitar.class);
                        Log.i("firebase","InstrumentCompany: " + classic.getInstrumentCompany());
                        Log.i("firebase","InstrumentModel: " + classic.getInstrumentModel());

                    }
                }

让chiildren计数有效,但其余的dosn't.i得到无法反弹以输入异常。这是logcat:

07-05 08:25:50.310: E/AndroidRuntime(32195): com.firebase.client.FirebaseException: Failed to bounce to type
07-05 08:25:50.310: E/AndroidRuntime(32195):    at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:185)
07-05 08:25:50.310: E/AndroidRuntime(32195):    at com.bobby.abcdefghijk.FireBaseTest$Bg$1.onDataChange(FireBaseTest.java:44)
07-05 08:25:50.310: E/AndroidRuntime(32195):    at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:56)
07-05 08:25:50.310: E/AndroidRuntime(32195):    at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
07-05 08:25:50.310: E/AndroidRuntime(32195):    at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
07-05 08:25:50.310: E/AndroidRuntime(32195):    at android.os.Handler.handleCallback(Handler.java:739)
07-05 08:25:50.310: E/AndroidRuntime(32195):    at android.os.Handler.dispatchMessage(Handler.java:95)
07-05 08:25:50.310: E/AndroidRuntime(32195):    at android.os.Looper.loop(Looper.java:158)
07-05 08:25:50.310: E/AndroidRuntime(32195):    at android.app.ActivityThread.main(ActivityThread.java:7224)
07-05 08:25:50.310: E/AndroidRuntime(32195):    at java.lang.reflect.Method.invoke(Native Method)
07-05 08:25:50.310: E/AndroidRuntime(32195):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
07-05 08:25:50.310: E/AndroidRuntime(32195):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
07-05 08:25:50.310: E/AndroidRuntime(32195): Caused by: com.shaded.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "InstrumentCompany" (class com.bobby.abcdefghijk.ClassicGuitar), not marked as ignorable (12 known properties: , "instrumentModel", "titleTag", "lastUpdatedPrice", "instrument", "instrumentUrl", "urlForImg", "updatedAt", "bids", "instrumentCompany", "objectId", "type", "createdAt"])
07-05 08:25:50.310: E/AndroidRuntime(32195):  at [Source: java.io.StringReader@4cefeee; line: 1, column: 23] (through reference chain: com.bobby.abcdefghijk.ClassicGuitar["InstrumentCompany"])
07-05 08:25:50.310: E/AndroidRuntime(32195):    at com.shaded.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:555)
07-05 08:25:50.310: E/AndroidRuntime(32195):    at com.shaded.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:708)
07-05 08:25:50.310: E/AndroidRuntime(32195):    at com.shaded.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1160)
07-05 08:25:50.310: E/AndroidRuntime(32195):    at com.shaded.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:315)
07-05 08:25:50.310: E/AndroidRuntime(32195):    at com.shaded.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
07-05 08:25:50.310: E/AndroidRuntime(32195):    at com.shaded.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
07-05 08:25:50.310: E/AndroidRuntime(32195):    at com.shaded.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
07-05 08:25:50.310: E/AndroidRuntime(32195):    at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:183)
07-05 08:25:50.310: E/AndroidRuntime(32195):    ... 11 more

0 个答案:

没有答案