Firebase数据库对象映射

时间:2018-01-09 12:24:14

标签: android firebase-realtime-database

我在从Google Firebase回读时遇到以下错误。

  

com.google.firebase.database.DatabaseException:预期映射时   反序列化,但得到了一个java.lang.Long类

我有一个Model类,即FBOrganisation。

    public class FBOrganisation {

    private String name;
    private String address;
    private String admin_email;
    private String subscription_type;
    private Boolean is_active;
    private String state;
    private String pin_code;
    private String id;
    private Map<String, String> created_on;

    public String getAdmin_email() {
        return admin_email;
    }

    public void setAdmin_email(String admin_email) {
        this.admin_email = admin_email;
    }

    public String getSubscription_type() {
        return subscription_type;
    }

    public void setSubscription_type(String subscription_type) {
        this.subscription_type = subscription_type;
    }

    public Boolean getIs_active() {
        return is_active;
    }

    public void setIs_active(Boolean is_active) {
        this.is_active = is_active;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getPin_code() {
        return pin_code;
    }

    public void setPin_code(String pin_code) {
        this.pin_code = pin_code;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Map<String, String> getCreated_on() {
        return created_on;
    }

    public void setCreated_on(Map<String, String> created_on) {
        this.created_on = created_on;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Exclude
    public Map<String, Object> toMap() {
        HashMap<String, Object> result = new HashMap<>();
        result.put("name", name);
        result.put("address", address);
        result.put("admin_email", admin_email);
        result.put("subscription_type", subscription_type);
        result.put("is_active", is_active);
        result.put("state", state);
        result.put("pin_code", pin_code);
        result.put("id", id);
        result.put("created_on", created_on);
        return  result;
    }
}

我用

在firebase中推送这个类
organisations.push().setValue(fbOrganisation, new DatabaseReference.CompletionListener() {
        @Override
        public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
            if(databaseError == null){
                databaseReference.child("id").setValue(databaseReference.getKey());

            }
        }
    });

当我尝试使用以下代码回读数据时,我收到了上述错误。

DatabaseReference organisations = mDatabase.getReference("organisations");
        organisations.orderByChild("id").equalTo(orgId).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                if (dataSnapshot.exists()) {
                    for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
                        FBOrganisation anOrganisation = singleSnapshot.getValue(FBOrganisation.class);
                        Log.d("TAG15",anOrganisation.getName());
                        Log.d("TAG15",anOrganisation.getId());
                    }                
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.e("TAG15", "onCancelled", databaseError.toException());
            }
        });

以下是Firebase数据库的快照。

enter image description here

1 个答案:

答案 0 :(得分:1)

private Map<String, String> created_on; POJO中有FBOrganisationcreated_on在db中包含长值