我目前正在使用Firebase數據庫處理項目,但無法將數據檢索爲列表。無法將列表檢索到Firebase數據庫
在這裏,我已導入火力的JSON的示例:
{
"mood" : [ {
"date" : 1480625911154,
"reasons" : "Lorem Ipsum",
"status" : "Ok",
"user" : "812c3772-5e3b-4776-8b4c-41ee3047011d"
}, {
"date" : 1480626081958,
"reasons" : "Lorem Ipsum",
"status" : "Ok",
"user" : "3fca7432-ac0b-40f8-b898-929df4d3a2e8"
}, {
"date" : 1480626567674,
"reasons" : "Lorem Ipsum",
"status" : "Ok",
"user" : "22e9254a-161b-4126-8cd2-7e5ebe03fa6f"
} ]
}
進入我的Android應用程序,我想用下面的查詢和代碼的特定用戶獲取數據(它是在科特林) :
FirebaseDatabase.getInstance().getReference("mood").orderByChild("user").equalTo("fa224092-2dfa-49c4-9fb6-c63eccf61a8b").addListenerForSingleValueEvent(object : ValueEventListener
{
override fun onCancelled(error: DatabaseError)
{
}
override fun onDataChange(snapshot: DataSnapshot)
{
moods = snapshot.getValue(object : GenericTypeIndicator<List<Mood>>(){
})
}
})
凡moods
聲明爲:private var moods = emptyList<Mood>()
和Mood
類(這是JAVA):
@IgnoreExtraProperties
public final class Mood
implements Serializable
{
public String user;
public String status;
public String reasons;
public long date;
public Mood()
{
super();
}
public Mood(String user, String status, String reasons, long date)
{
this.user = user;
this.status = status;
this.reasons = reasons;
this.date = date;
}
}
每次,對DataSnapshot
轉化到一個列表與下列堆棧跟蹤應用程序崩潰:
十月12日至2日:20:01.497 22282-22282/com.test.app E/AndroidRuntime :致命 EXCEPTION:主進程:com.test.app,PID:22282 com.google.firebase.database.DatabaseException:期望一個列表,而 反序列化,但在com.google獲得類java.util.HashMap 。 android.gms.internal.zzbqi.zza(Unknown Source) at com.google.android.gms.internal.zzbqi.zza(Unknown Source) at com.google.android.gms.internal.zzbqi.z ZA(來源不明) 在com.google.firebase.database.DataSnapshot.getValue(來源不明) 在com.test.app.fragment.StatsFragment $ onRetrieveBusinessObjects $ 1.onDataChange(StatsFragment.kt:46) 在com.google .firebase.database.Query $ 1.onDataChange(Unknown Source) at com.google.android.gms.internal.zzbmz.zza(Unknown Source) at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source ) at com.google.android.gms.internal.zzboc $ 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)
我不明白爲什麼一個HashMap
檢索......事實上,在以前的項目,與類似的代碼,火力地堡給我一個lsit預期。
爲了做一個比較,這裏是前一個項目的代碼(在java中)。
{
"books" : [ {
"cartoonist" : "Test1",
"id" : "10",
"image" : "http://static.fnac-static.com/multimedia/Images/FR/NR/22/e1/7e/8315170/1507-1.jpg",
"price" : "15.95",
"publication" : "2016-11-01T00:00:00+01:00",
"summaryExtract" : "Loremp Ipsum",
"summaryFull" : "Loremp Ipsum",
"tome" : "Tome 24 : Le testament de William S"
}, {
"cartoonist" : "Riad Sattouf",
"id" : "1",
"image" : "http://static.fnac-static.com/multimedia/Images/FR/NR/da/9d/7c/8166874/1507-1.jpg",
"price" : "20.90",
"publication" : "2016-10-01T00:00:00+01:00",
"summaryExtract" : "Loremp Ipsum",
"summaryFull" : "Loremp Ipsum",
"tome" : "Tome 3 : L'Arabe du futur"
}]
}
下面的代碼(在Java),查詢火力地堡:
已導入到火力地堡數據庫中的JSON
FirebaseDatabase.getInstance().getReference().child("books").addListenerForSingleValueEvent(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
final List<Book> books = dataSnapshot.getValue(new GenericTypeIndicator<List<Book>>()
{
});
}
@Override
public void onCancelled(DatabaseError databaseError)
{
}
});
而且Book
類的代碼:
@IgnoreExtraProperties
public final class Book
implements Serializable
{
public String id;
public String title;
public String tome;
public String cartoonist;
public String publication;
public String price;
public String image;
public String summaryFull;
public String summaryExtract;
public Book()
{
super();
}
public Book(String id, String cartoonist, String image, String price, String publication, String summaryExtract,
String summaryFull, String title, String tome)
{
this.id = id;
this.cartoonist = cartoonist;
this.image = image;
this.price = price;
this.publication = publication;
this.summaryExtract = summaryExtract;
this.summaryFull = summaryFull;
this.title = title;
this.tome = tome;
}
@Exclude
public String getFormattedDate()
{
final DateTime dateTime = new DateTime(publication);
return dateTime.monthOfYear().getAsText(Locale.getDefault()) + " " + dateTime.getYear();
}
該代碼按預期工作。
我錯過了Firebase數據庫中的查詢嗎?有沒有可能將我的數據檢索爲列表而不是散列表?
謝謝你的幫助!
謝謝你,它的工作原理!所以問題是爲什麼我的代碼在一個項目中工作,而不是另一個.. :) – rolandl