2016-10-15 83 views
1

我閱讀了有關此問題的幾個答案,但我仍然在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" : "" 
    } 
    } 
} 
+0

你已經在你的問題中包含了JSON樹的圖片。請用實際的JSON替換爲文本,您可以通過點擊Firebase數據庫控制檯中的導出按鈕輕鬆獲取。將JSON作爲文本可以搜索,使我們能夠輕鬆使用它來測試您的實際數據,並將其用於我們的答案中,並且通常只是一件好事。 –

+0

你不會在'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 –

+0

我按照你的建議添加了這個方法,但是仍然返回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

回答

2

我解決了改變數據的路徑進入火力地堡:

到類UserGu:

  1. 我忘了寫getter getUserUid()

    public String getUserUid(){ return userUid; }數據庫火力地堡,因爲我想讀取數據的

2.道路是不正常: 我改爲: mFirebaseDatabaseReference.child(「用戶」)子(mUserUid).setValue( userClassGu);

UserClassGu userClassGu = UserClassGu(mUserUid,mUserEmail,mUsername,""); 



      mFirebaseDatabaseReference.child("users").child(mUserUid).setValue(userClassGu); 


      //Write data of users 
      mFirebaseDatabaseReference.child("users").child(mUserUid).setValue(userClassGu); 


      //Reading the data 


mFirebaseDatabaseReference.child("users").child(mUserUid).addValueEventListener(
       new ValueEventListener() { 
        @Override 
        public void onDataChange(DataSnapshot dataSnapshot) { 
         // Get user value 
         UserClassGu userClassgu = dataSnapshot.getValue(UserClassGu.class); 
         Log.d(TAG,"reading data :userClassgu.email = "+userClassgu.email); 
         Log.d(TAG,"reading data :userClassgu.name: = "+userClassgu.name); 
         Log.d(TAG,"reading data :userClassgu.telephone: = "+userClassgu.telephone); 
         Log.d(TAG,"reading data :userClassgu.userUid: = "+userClassgu.userUid); 
         // ... 
        } 

        @Override 
        public void onCancelled(DatabaseError databaseError) { 
         Log.w(TAG, "getUser:onCancelled", databaseError.toException()); 
         // ... 
        } 
       }); 

在課堂上userClassGu我把方法:

@Exclude 
public Map<String, Object> toMap() { 
    HashMap<String, Object> result = new HashMap<>(); 
    result.put("uid", userUid); 
    result.put("email", email); 
    result.put("name", name); 
    result.put("telephone", telephone); 
    return result; 
} 

我把詮釋類UserClassGuthe文本: @IgnoreExtraProperties

類userClassGu的代碼變成了:

package com.sciencesoftware.friends; 

import com.google.firebase.database.Exclude; 
import com.google.firebase.database.IgnoreExtraProperties; 

import java.util.HashMap; 
import java.util.Map; 

/** 
* Created by Cliente on 14/10/2016. 
*/ 
@IgnoreExtraProperties 
public class UserClassGu { 
    public String userUid; 
    public String email; 
    public String name; 
    public String telephone; 

    public UserClassGu() { 
// Default constructor required for calls to DataSnapshot.getValue(UserClassGu.class) 

    } 

    public UserClassGu(String userUid, String email, String name, String telephone) { 
     this.userUid = userUid; 
     this.email = email; 
     this.name = name; 
     this.telephone = telephone; 
    } 


    public String getUserUid() { 
     return userUid; 
    } 





    public void setUserUid(String userUid) { 
     this.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; 
    } 


    @Exclude 
    public Map<String, Object> toMap() { 
     HashMap<String, Object> result = new HashMap<>(); 
     result.put("uid", userUid); 
     result.put("email", email); 
     result.put("name", name); 
     result.put("telephone", telephone); 
     return result; 
    } 



} 

3。 這是火力地堡的JSON:

{ 
    "users" : { 
    "Bl0AiMMUXsV2H58lqoj0rO8457j1" : { 
     "email" : "[email protected]", 
     "name" : "my name", 
     "telephone" : "", 
     "userUid" : "Bl0AiMMUXsV2H58lqoj0rO8457j1" 
    } 
    } 
} 

天花板高度的文檔: 類,將有投入必須與數據庫中的數據: 1有一個構造空: 2有一個構造帶的所有參數變量 - (我對此不完全正確) 該類的變量必須是公開的,或者必須公開所有getter(我對此不完全正確)。 3擁有所有的獲得者。4二傳手沒有必要。

如果有人糾正我,我會很高興,如果我說上述4件事情有問題。