我閱讀了有關此問題的幾個答案,但我仍然在Firebase Android應用的答案中爲空。其中之一是:返回存儲在Firebase中的值Android
Firebase retrieve/read returns null values - Android
我的問題:
我可以寫成火力地堡,但我無法讀出值。
我嘗試:的火力地堡規則
1變化的值從:
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
到:
{
"rules": {
".read": true,
".write": true
}
}
,但沒有工作,我可以寫,但不能讀取數值。
我的代碼:
private void postComment() {
//final String uid = getUid();
FirebaseDatabase.getInstance().getReference().child("users")/*.child("KU1y_SLZGLZpJB2j_Pc")*/
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get user information
UserClassGu user = dataSnapshot.getValue(UserClassGu.class);
String authorName = user.name;
Log.d(TAG,"name read into postCopmment = "+authorName);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
這在的getValue(UserClassGu.class)用我的類
/** * 創建者Cliente上14/10/2016。 */
public class UserClassGu {
public String userUid;
public String email;
public String name;
public String telephone;
public UserClassGu() {
}
public String getUserUid() {
return userUid;
}
public UserClassGu(String userUid, String email, String name, String telephone) {
userUid = userUid;
this.email = email;
this.name = name;
this.telephone = telephone;
}
public void setUserUid(String userUid) {
userUid = userUid;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
}
我的數據庫:
{
"messages" : {
"-KU87z9hSXew-Li37xgF" : {
"destinatario" : "[email protected]",
"name" : "app app",
"photoUrl" : "https://lh5.googleusercontent.com/-jjv4PSa0NzA/AAAAAAAAAAI/AAAAAAAAAAA/APaXHhQm8WxqRupmfbe87U11m9pfn9C9ag/s96-c/photo.jpg",
"text" : "hello"
},
"-KU88ciYGXz39PZGZW98" : {
"destinatario" : "rere",
"name" : "app app",
"photoUrl" : "https://lh5.googleusercontent.com/-jjv4PSa0NzA/AAAAAAAAAAI/AAAAAAAAAAA/APaXHhQm8WxqRupmfbe87U11m9pfn9C9ag/s96-c/photo.jpg",
"text" : "adafa"
}
},
"messages_private" : {
"rere" : {
"553xVAMEVcQZPKNVqfal8S9tOM03" : {
"-KU88ciZUNQkCskad6Xc" : {
"destinatario" : "rere",
"name" : "app app",
"photoUrl" : "https://lh5.googleusercontent.com/-jjv4PSa0NzA/AAAAAAAAAAI/AAAAAAAAAAA/APaXHhQm8WxqRupmfbe87U11m9pfn9C9ag/s96-c/photo.jpg",
"text" : "adafa"
}
}
}
},
"users" : {
"-KU3ZlBN-gmHZOhg74-M" : {
"email" : "[email protected]",
"name" : "app app",
"telephone" : ""
},
"-KU3_ZjTHJHprd36xT1u" : {
"email" : "[email protected]",
"name" : "app app",
"telephone" : ""
}
}
}
你已經在你的問題中包含了JSON樹的圖片。請用實際的JSON替換爲文本,您可以通過點擊Firebase數據庫控制檯中的導出按鈕輕鬆獲取。將JSON作爲文本可以搜索,使我們能夠輕鬆使用它來測試您的實際數據,並將其用於我們的答案中,並且通常只是一件好事。 –
你不會在'onCancelled'中處理錯誤。請參閱http://stackoverflow.com/documentation/firebase/5548/how-do-i-listen-for-errors-when-accessing-the-database/22652/detect-errors-when-reading-data-on-android #t = 201610150548314420923 –
我按照你的建議添加了這個方法,但是仍然返回null:public void onCancelled(DatabaseError databaseError)System.out.println(「The read failed:」+ databaseError.getCode()); Log.w(TAG,「loadPost:onCancelled」,databaseError.toException()); throw databaseError.toException(); – Gustavomcls