2017-04-18 58 views
1

我正在使用Firebase進行身份驗證和實時數據庫。我的驗證碼已成功也跑我的輸入值,數據庫代碼也運行,但是當我編碼在數據庫中獲取價值,我得到運行時錯誤嘗試在火力地堡數據庫輸入值:獲取數據庫時無效的Firebase路徑錯誤

FATAL EXCEPTION: main 
Process:com.xxx.xxx, PID: 22601 
com.google.firebase.database.DatabaseException: Invalid Firebase Database 
path: https://xxx-exxe.firebaseio.com/. Firebase Database paths must not contain '.', '#', '$', '[', or ']' 

我的代碼是:

final FirebaseDatabase database = FirebaseDatabase.getInstance(); 
DatabaseReference reference = database.getReference("https://korsa-e03ae.firebaseio.com/"); 

reference.addValueEventListener(new com.google.firebase.database.ValueEventListener() { 
    @Override 
    public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) { 
     Offerride user = dataSnapshot.getValue(Offerride.class); 

     if (user == null){ 
      Toast.makeText(getContext(),"User data is null!",Toast.LENGTH_LONG).show(); 
      return; 
     } 

     tvsource.setText(user.source + " , " + user.destination + " , " + user.startDate + " , " + user.startTime); 

    } 

    @Override 
    public void onCancelled(DatabaseError databaseError) { 
     Toast.makeText(getContext(), "Failefddd", Toast.LENGTH_LONG).show(); 
    } 
}); 
+0

爲什麼是它的'ValueEventListener()'和'DataSnapshot'正與'com.google.firebase.database.'前綴使用?刪除它時會拋出錯誤嗎? –

+0

您的firebase路徑值包含以下字符之一'。','#','$','['或']'.so引用路徑不包含這些字符。當您呼籲這條道路的孩子 –

回答

3

我想答案是很明顯的,因爲應用程序是你並不需要特定的url已鏈接到database當您設置該項目

剛剛從

DatabaseReference reference = database.getReference("https://korsae03ae.firebaseio.com/");

改變

DatabaseReference reference = database.getReference(); 那麼它應該工作

0

數據庫的網址位於您的google-services.json文件中。 通過火力文檔https://firebase.google.com/docs/database/admin/retrieve-data讀取數據,你可以做到以下幾點:

final FirebaseDatabase database = FirebaseDatabase.getInstance(); 
DatabaseReference ref = database.getReference("posts"); 

// Attach a listener to read the data at our posts reference 
ref.addValueEventListener(new ValueEventListener() { 
@Override 
public void onDataChange(DataSnapshot dataSnapshot) { 
    Post post = dataSnapshot.getValue(Post.class); 
    System.out.println(post); 
} 

@Override 
public void onCancelled(DatabaseError databaseError) { 
    System.out.println("The read failed: " + databaseError.getCode()); 
} 
}); 
相關問題