Android Firebase检索数据

时间:2016-10-02 09:23:45

标签: android firebase firebase-realtime-database

在我的Android应用中,我正在尝试从Firebase后端获取活动用户列表。用户在Firebase中保存为名为UserActive的Java类,其中包含有关它们的详细信息。

我以为我做的一切都很好,但是我收到错误并且应用程序崩溃了。我的代码:

ChildEventListener activeUserschildEventListener = new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {

                    UserActive uA = userSnapshot.getValue(UserActive.class);

                    Toast.makeText(MapsActivity.this, uA.getUsername(), Toast.LENGTH_LONG).show();

                }

            }

我的错误讯息:

FATAL EXCEPTION: main
Process: shomrim.warpcodes.com.shomrim, PID: 28835
    com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type shomrim.warpcodes.com.shomrim.UserActive
    at com.google.android.gms.internal.zzaln.zzd(Unknown Source)
    at com.google.android.gms.internal.zzaln.zzb(Unknown Source)
    at com.google.android.gms.internal.zzaln.zza(Unknown Source)
    at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
    at shomrim.warpcodes.com.shomrim.MapsActivity$2.onChildAdded(MapsActivity.java:195)
    at com.google.android.gms.internal.zzahe.zza(Unknown Source)
    at com.google.android.gms.internal.zzaje.zzcta(Unknown Source)
    at com.google.android.gms.internal.zzajh$1.run(Unknown Source)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我的Firebase结构如下:

enter image description here

显然,UserActive类缺少一些东西。如果可以,请你帮助我。感谢。

我的UserActive类:

package shomrim.warpcodes.com.shomrim;

import android.location.Location;

/**
 * Created by Gil on 09/18/16.
 */

public class UserActive {

    String username;
    Location userlocation;
    String usericoninmap;
    String userphonenumber;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Location getUserlocation() {
        return userlocation;
    }

    public void setUserlocation(Location userlocation) {
        this.userlocation = userlocation;
    }

    public String getUsericoninmap() {
        return usericoninmap;
    }

    public void setUsericoninmap(String usericoninmap) {
        this.usericoninmap = usericoninmap;
    }

    public String getUserphonenumber() {
        return userphonenumber;
    }

    public void setUserphonenumber(String userphonenumber) {
        this.userphonenumber = userphonenumber;
    }
}

我的数据库参考:

groupPosShareRef = mRootRef.child("groups")
        .child(currentUserGroupsString)
        .child("groupPosData");

从Firebase导出的JSON(删除userlocation部分后:

"groups" : {
    "-KRietwzwz_5c2oGI0Z8" : {
      "groupPosData" : {
        "DDJxiDshfwbJFLxHXnMg7V5fHqy1" : {
          "username" : "username"
        }
      }

我尝试将UserActive POJO更改为不包含Location对象的POJO,而不是lat和lon的简单String。它被称为UsersActive

package shomrim.warpcodes.com.shomrim;

/**
 * Created by user on 10/02/16.
 */

public class UsersActive {

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUserlat() {
        return userlat;
    }

    public void setUserlat(String userlat) {
        this.userlat = userlat;
    }

    public String getUserlon() {
        return userlon;
    }

    public void setUserlon(String userlon) {
        this.userlon = userlon;
    }

    public String getUsericoninmap() {
        return usericoninmap;
    }

    public void setUsericoninmap(String usericoninmap) {
        this.usericoninmap = usericoninmap;
    }

    public String getUserphonenumber() {
        return userphonenumber;
    }

    public void setUserphonenumber(String userphonenumber) {
        this.userphonenumber = userphonenumber;
    }

    String username;
    String userlat;
    String userlon;
    String usericoninmap;
    String userphonenumber;

}

仍然应用程序在同一行崩溃并出现相同的错误:

            UsersActive uA = userSnapshot.getValue(UsersActive.class);

新的Firebase JSON:

"groups" : {
    "-KRietwzwz_5c2oGI0Z8" : {
      "groupPosData" : {
        "DDDDDDD" : {
          "username" : "username1"
        },
        "DDJxiDshfwbJFLxHXnMg7V5fHqy1" : {
          "username" : "username2"
        }
      }

1 个答案:

答案 0 :(得分:4)

解决了它。

问题是我使用了ChildEventListener,而我应该使用简单的ValueEventListener。

当我意识到我得到的数据只是一步而且太深了时,就明白了这一点。比我需要的还要多。

感谢Nishant的所有帮助!