我正在尝试检查已登录用户是否是Firebase数据库中JSON阵列的授权用户。如何从Firebase获取电子邮件字段的值。 ?
这是模型 User
类:
public class User {
ArrayList<User> email;
public User() {}
public ArrayList<User> getEmail() {
return email;
}
public void setEmail(ArrayList<User> email) {
this.email = email;
}
}
为了从email
获取userslist
,我使用了这个逻辑。
usersListDatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User userEmail = dataSnapshot.getValue(User.class);
ArrayList<String> userEmailFromDatabase = userEmail.getEmail();
Log.d(TAG, "User email from Database: "+userEmailFromDatabase);
Log.d(TAG, "Logged in User email: " + user.getEmail());
}
但是当我尝试从数据库中获取电子邮件时,我收到以下异常,我的应用程序崩溃了。
com.google.firebase.database.DatabaseException: Can't convert object of type java.util.ArrayList to type com.apvolution.amapp.model.User
如何获得'email from the
userslist field ? How do I loop through the entire
usersList`?
这是我的Firebase控制台中的数据库结构。
amapp-731b2addclose
+ messages
+ version
- userslist
- email
|
| -- 0: "abc@gmail.com"
| -- 1: "xyz@gmail.com"
| -- 2: "ghi@gmail.com"
| -- 3: "mno@gmail.com"
LogCat详细信息:
05 - 13 21: 59: 37.418 16387 - 16387 / com.apvolution.bcm E / AndroidRuntime: FATAL EXCEPTION: main
Process: com.apvolution.bcm, PID: 16387
com.google.firebase.database.DatabaseException: Can 't convert object of type java.lang.String to type com.apvolution.amapp.model.User
at com.google.android.gms.internal.zzbtg.zze(Unknown Source)
at com.google.android.gms.internal.zzbtg.zzb(Unknown Source)
at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.apvolution.bcm.MainActivity$3.onDataChange(MainActivity.java: 260)
at com.google.android.gms.internal.zzbpx.zza(Unknown Source)
at com.google.android.gms.internal.zzbqx.zzZT(Unknown Source)
at com.google.android.gms.internal.zzbra$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java: 751)
at android.os.Handler.dispatchMessage(Handler.java: 95)
at android.os.Looper.loop(Looper.java: 154)
at android.app.ActivityThread.main(ActivityThread.java: 6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 757)
答案 0 :(得分:1)
您可以尝试将数据存储在ArrayList
而不是Map
中。更改Map<String, Object> map = new HashMap<>();
中的类型,您的问题就会得到解决。
要从dataSnapshot
对象中获取数据,请使用以下代码:
DatabaseReference yourRef = FirebaseDatabase.getInstance().getReference().child("userslist").child("email");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String firstEmail = (String) dataSnapshot.child("0").getValue();
String secondEmail = (String) dataSnapshot.child("1").getValue();
String thirdEmail = (String) dataSnapshot.child("3").getValue();
String fourthEmail = (String) dataSnapshot.child("4").getValue();
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
yourRef.addListenerForSingleValueEvent(eventListener);
希望它有所帮助。
答案 1 :(得分:0)
似乎DataSnapshot
的{{1}}要求您提交getValue(GenericTypeIndicator)
而非List<YourCustomModel>
,请参阅this link from Firebase docs。这意味着你应该这样做:
YourCustomModel
然后,从那里,单独检索每封电子邮件。您可能想要重新检查您的用户模型的执行方式,因为它可能不需要它自己的GenericTypeIndicator<List<User>> t = new GenericTypeIndicator<List<User>>() {};
List<User> userEmail = dataSnapshot.getValue(t);