SANJAY GUPTA SANJAY GUPTA - 1 month ago 6
Android Question

Data is not saving using Firebase

I am using following code to save data on firebase . But it is giving permission denied error.Here is my code. I have followed all instruction given by firebase.

buttonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Firebase ref = new Firebase(Config.FIREBASE_URL);
String name = editTextName.getText().toString().trim();
String address = editTextAddress.getText().toString().trim();
Person person = new Person();
person.setName(name);
person.setAddress(address);
ref.child("Person").setValue(person);
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
Person person = postSnapshot.getValue(Person.class);
String string = "Name: " + person.getName() + "\nAddress: " + person.getAddress() + "\n\n";
textViewPersons.setText(string);
}
}

@Override
public void onCancelled(FirebaseError firebaseError) {
Log.e("The read failed: ", firebaseError.getMessage());
}
});

}
});

Answer

You might have following rule in your Rules Tab:

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

This rule means only authenticated users would be able to perform read and write operations.

Just change it to:

{
  "rules": {
    ".read": "true",
    ".write": "true"
  }
}

Caution: This rule means that anyone can read and write to your firebase database.

Steps: Go to Firebase Console -> Select Project -> Select Database from left handside menu -> Click on Rules tab -> Copy the rules over there.

Comments