0
我目前正在使用firebase存儲所有信息,我使用firebase身份驗證進行了用戶註冊,並將該用戶擴展爲重用身份驗證中創建的ID的數據庫,現在我需要獲取所有用戶並進行比較他的手機與手機中插入與用戶和retrive特定用戶,如果號碼匹配,所以我這樣做:數據快照失敗轉換
代碼
FirebaseDatabase.getInstance().getReference().child("Users")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Log.d("snapshot", String.valueOf(snapshot));
Log.d("snap",String.valueOf(snapshot.getValue(User.class)));
User user = snapshot.getValue(User.class);
if(number.equals(String.valueOf(user.getPhone()))){
userFriend = user;
}
}
if(userFriend == null){
//to do snackbar
}
}
User類
public class User {
private String id;
private String username;
private int age = -1;
private String phone;
private int gamesPlayed = 0;
private ArrayList<User> friends = new ArrayList<User>();
// CONSTRUCTOR
public User() {
}
public User(String username) {
this.username = username;
}
// GETTERS & SETTERS
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public User(String id, String username, int age, String phone, int gamesPlayed, ArrayList<User> users) {
this.id = id;
this.username = username;
this.age = age;
this.phone = phone;
this.gamesPlayed = gamesPlayed;
this.friends = users;
}
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
我的數據庫結構:
這裏是我所得到的堆棧跟蹤:
04-20 16:06:04.070 30972-30972/com.esmad.pdm.friendlymanager E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.esmad.pdm.friendlymanager, PID: 30972
com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String
at com.google.android.gms.internal.zzbtg.zzaF(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.zzb(Unknown Source)
at com.google.android.gms.internal.zzbtg$zza.zze(Unknown Source)
at com.google.android.gms.internal.zzbtg$zza.zzaH(Unknown Source)
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.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.esmad.pdm.friendlymanager.Friends$1.onDataChange(Friends.java:61)
at com.google.firebase.database.Query$1.onDataChange(Unknown Source)
at com.google.android.gms.internal.zzbpx.zza(Unknown Source)
at com.google.android.gms.internal.zzbqx.zzZS(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:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main
(ZygoteInit.java:776)
錯誤與此行有關:User user = snapshot.getValue(User.class);
我懷疑它對我構建我的用戶模型和檢索的數據的方式有所影響,但某些內容不兼容。我有2 int在我的模型gamesPlayed和年齡,但是當我將用戶傳遞到快照我不知道如何CAS 2特定的值,所以我可以通過他們做我需要redifine用戶實例嗎?或者是一切正常,請幫助我很困惑:/`
爲什麼它不能是字符串類型? –
因爲您沒有將它作爲字符串存儲在數據庫中,所以如果您想要將其作爲字符串檢索,請將數字之間的電話號碼放在數據庫中的引號之間,如「91351246」。我強烈建議您將它們保存爲字符串,以便稍後格式化它們 – wnieves19