Firebase数据库从Firebase数据库检索数据时出现异常

时间:2017-06-30 10:18:49

标签: android firebase firebase-realtime-database firebase-authentication

我有一个Firebase数据库,如下所示。我正在尝试检索此剧集数据但总是收到错误

users:
{
 Fcm_id: "c68y2j3bwis:APA91bFABxC6W5FmYVCOSmgJiB_-UEqnrmQ...",
 Name: "Vikas Kumar",
 episodes:{
 0: "-Kno0IYdlSjjfFsXkGQ7",
 1: "-Kno0IYdlSjjfFsXkGQ8"
 },
 gender:Male,
 age:14,
 email:"vikas@gmail.com",
}

这是我正在创建HashMap的用户POJO类

public class User implements Serializable{

    private String Name;
    private String email;
    private String gender;
    private String Fcm_id;
    private String id;
    private int age;
    private String image;

    private ArrayList<Map<String,Object>> episodes;



public User()
{

}

public ArrayList<Map<String, Object>> getEpisodes() {
    return episodes;
}

public void setEpisodes(ArrayList<Map<String, Object>> episodes) {
    this.episodes = episodes;
}


    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getFcm_id() {
        return Fcm_id;
    }

    public void setFcm_id(String Fcm_id) {
        this.Fcm_id = Fcm_id;
    }

    public String getId() {
        return id;
    }

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

    public String getName() {
        return Name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

}

ListPatients.java类 我编写代码来从数据库中检索数据

 firebasePathMain.child("users").child(auth.getCurrentUser().getUid()).child("episodes").addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                String episodeId = dataSnapshot.getValue().toString();
                Log.d(TAG,episodeId);

                firebasePathMain.child("episodes").child(episodeId).child("Participants").child("User_id").addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        final String UserId = dataSnapshot.getValue().toString();
                        Log.d(TAG,UserId);

                        firebasePathMain.child("users").child(UserId).addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                User user = dataSnapshot.getValue(User.class);
                                user.setId(UserId);

                                Log.d(TAG,user.getName());
                                listPatient.add(user);
                                adapterPatients.notifyDataSetChanged();

                               // adapterPatients.refillAdapter(user);
                              //  listUpdate.add(0, user.getId());

                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });

                        Listpatients.add(UserId);
                        Log.d(TAG,Listpatients.toString());

                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

            }

            @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(DatabaseError databaseError) {

            }
        });

但是当我尝试检索数据时,就是这个错误

在反序列化时期望一个Map,但得到一个类java.util.ArrayList

1 个答案:

答案 0 :(得分:0)

您需要使用SELECT t4.resource_id, t4.name, t4.parent_id, t4.perms, t4.url, (SELECT t3.name FROM sys_resource t3 WHERE t3.resource_id = t4.parent_id) AS parentName FROM sys_resource t4 WHERE EXISTS( SELECT t2.resource_id FROM sys_role_resource t2 WHERE EXISTS( SELECT t1.role_id FROM sys_user_role t1 WHERE t1.user_id = 1)) ORDER BY t4.order_num ASC; ,而不是Map<String, Object>

请使用此代码:

HashMap<Integer, String>

要查询您的数据库,请使用以下代码:

private Map<String, Object> episodes;

public HashMap<String, Object> getEpisodes() {
    return episodes;
}

public void setEpisodes(HashMap<String, Object> episodes) {
    this.episodes = episodes;

}

输出将是:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference episodesRef = rootRef..child("users").child(auth.getCurrentUser().getUid()).child("episodes");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String key = ds.getKey();
            String value = ds.getValue(String.class)
            Log.d("TAG", "key= " + key + " -> value: " + value);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
episodesRef.addListenerForSingleValueEvent(eventListener);

它肯定会起作用。