2016-09-17 12 views
1

好的,所以我希望有人可以幫助我解決這個問題,因爲我過去幾天一直在嘗試一堆不同的解決方案。在我的活動中,我有一個EditText和一個Button,並且正在嘗試將該用戶輸入寫入Firebase。當我運行應用程序時,我沒有遇到任何錯誤,但是,當我按下按鈕時,沒有任何反應。數據沒有寫到Firebase的按鈕按

這裏是我的類:

private EditText userEmail; 
Button sendInviteButton; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Firebase.setAndroidContext(this); 
    setContentView(R.layout.activity_user_registration); 

    userEmail = (EditText) findViewById(R.id.user_email); 
    sendInviteButton = (Button) findViewById(R.id.sendInviteButton); 

} 

public void btn(View view) { 
    sendInviteButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Firebase ref = new Firebase("https://app-1318.firebaseio.com/clients"); 
      String enteredUserEmail = userEmail.getText().toString(); 
      ref.child("userBackgroundCheck").setValue(enteredUserEmail); 
     } 
    }); 
} 

我有一個onClick用於XML按鈕,所以我真的不知道爲什麼這個數據沒有被髮送到火力地堡。

最後,是我們應該使用根URL進行火力地堡這個部分?:

Firebase ref = new Firebase("https://app-1318.firebaseio.com/clients"); 

具體做法是:

Firebase ref = new Firebase("https://app-1318.firebaseio.com"); 

回答

1

嘗試去你的火力地堡控制檯和編輯你的數據庫規則 。 以下是圖片樣本。

Change your rules to that and see

+0

好吧,我現在要試一試。 – mur7ay

+0

工作,謝謝! – mur7ay

+0

更改這樣的規則使您的數據庫可讀並可寫入世界上的每個人。雖然這是您剛開始開發時的常見做法,但建議您儘快將代碼更改爲使用[Firebase身份驗證](https://firebase.google.com/docs/auth/)。一旦用戶通過身份驗證(即使[匿名](https://firebase.google.com/docs/auth/)),您也可以開始[保護他們訪問您的數據庫](https://firebase.google。 COM /文檔/數據庫/安全/)。 –

2

你有沒有試過在實時數據庫添加規則。當您轉到實時數據庫時,請單擊「規則」選項卡。添加下面的行

// These rules give anyone, even people who are not users of your app, 
// read and write access to your database 
{ 
    "rules": { 
    ".read": true, 
    ".write": true 
    } 
} 

看看它是否工作!

另外,你爲什麼要以舊的方式存儲數據。這裏是用於在實時數據庫中保存數據的new doc

+0

在我看到您的解決方案之前,我剛剛開始工作。我肯定會檢查你提供的鏈接。謝謝! – mur7ay

+0

很高興你的問題解決了! – Programmer