1

I'm trying to use Firebase Realtime database following Firebase's guide online but I can't get any updates on the database as my permission is being denied. I am not sure what I'm doing wrong. I've even set the rules to read and write to true.

The code inside onClickListener is below:

Button vDetails = (Button) v.findViewById(R.id.enter_vechile_details);
vDetails.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        DatabaseReference myRef = FirebaseDatabase.getInstance().getReference();
        myRef.child("Customers").child("Drivers").setValue("Name").addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.d(TAG, "signUp: set customer name" + e);
                e.printStackTrace();
            }
        });
        getFragmentManager().beginTransaction().replace(R.id.fragment_container, new SignUpCustomerFragment()).commit();
    }
});

This is inside a fragment and the button completes those statements first before it begins the transaction for the fragment replacement.

This is the rules in Firebase:

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

As I am new to JSON, I've also tried with a comma at the end of ".write"

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

This is the stacktrace:

09-10 04:32:46.274 16594-16594/com.buildingstories.sabbib.xpose W/System.err: com.google.firebase.database.DatabaseException: Firebase Database error: Permission denied
09-10 04:32:46.274 16594-16594/com.buildingstories.sabbib.xpose W/System.err:     at com.google.firebase.database.DatabaseError.toException(Unknown Source)
09-10 04:32:46.274 16594-16594/com.buildingstories.sabbib.xpose W/System.err:     at com.google.android.gms.internal.zzall$1.onComplete(Unknown Source)
09-10 04:32:46.274 16594-16594/com.buildingstories.sabbib.xpose W/System.err:     at com.google.android.gms.internal.zzahq$20.run(Unknown Source)
09-10 04:32:46.274 16594-16594/com.buildingstories.sabbib.xpose W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
09-10 04:32:46.274 16594-16594/com.buildingstories.sabbib.xpose W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
09-10 04:32:46.274 16594-16594/com.buildingstories.sabbib.xpose W/System.err:     at android.os.Looper.loop(Looper.java:168)
09-10 04:32:46.274 16594-16594/com.buildingstories.sabbib.xpose W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5885)
09-10 04:32:46.274 16594-16594/com.buildingstories.sabbib.xpose W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
09-10 04:32:46.274 16594-16594/com.buildingstories.sabbib.xpose W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
09-10 04:32:46.274 16594-16594/com.buildingstories.sabbib.xpose W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
09-10 04:32:46.274 16594-16594/com.buildingstories.sabbib.xpose W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
09-10 04:32:46.274 16594-16594/com.buildingstories.sabbib.xpose W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
09-10 04:32:46.276 16594-16594/com.buildingstories.sabbib.xpose W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection

I've been at this all day and I have no idea what's wrong since I followed the Firebase guide.

Daniel
  • 2,355
  • 9
  • 23
  • 30
Sabbib
  • 47
  • 2
  • 9
  • attach a `addOnFailureListener` to `myRef.setValue()` to see what error you are getting. – M.Waqas Pervez Sep 09 '16 at 12:18
  • post you Customer class code and real time database structure from firebase console – Burhanuddin Rashid Sep 09 '16 at 12:36
  • Most likely you don't have permission to write the data. Read this explanation on [how to detect errors when writing a value on Android](http://stackoverflow.com/documentation/firebase/5548/how-do-i-listen-for-errors-when-accessing-the-database#t=201609091416264266996) – Frank van Puffelen Sep 09 '16 at 14:17
  • You are right, I am not getting permission. I have set the rules to allow read and write data but its still not allowing me. anything I can do to debug it further? I have a printStackTrace but not making much of it @FrankvanPuffelen – Sabbib Sep 09 '16 at 18:05
  • refer this link it will work for me http://stackoverflow.com/a/37404116/4427519 – Dhara Patel Feb 01 '17 at 05:25

4 Answers4

0

You need to create a child for "drive" and push the Customer class as given below code and remember to create Empty Constructor in your Customer Class:

       FirebaseDatabase database = FirebaseDatabase.getInstance();
       DatabaseReference myRef = database.getReference("Drivers");

       Customer customer = new Customer();
       customer.setFirstName(fullName.getText().toString());//fullName is EditText

       myRef.child("drive").push().setValue(customer);
Burhanuddin Rashid
  • 5,260
  • 6
  • 34
  • 51
0

You have set topic in child for update value

Pass Value in constructor don't set.

Customer customer = new Customer(fullName.getText().toString()); // Pass values here in constructor
//customer.setFirstName(fullName.getText().toString());

myRef.child("Drivers").setValue(customer);
Uttam Panchasara
  • 5,735
  • 5
  • 25
  • 41
0

You should use HashMap to push your values as and add a onFailureListener so you can see what error you are getting:

 FirebaseDatabase database = FirebaseDatabase.getInstance();
       DatabaseReference myRef = database.getReference("Drivers");

       Customer customer = new Customer();
       customer.setFirstName(fullName.getText().toString());//fullName is EditText

       Map<String,Object> map = new HashMap<String,Object>();
       map.add("drive",customer);

       myRef.push().setValue(map).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            e.printStackTrace();
        }
    });

NOTE: Make sure Customer class has an empty constructor or Firebase won't be able to Serialize your class.

M.Waqas Pervez
  • 2,492
  • 2
  • 19
  • 33
0

Below code will set the Drivers class to "drive" .

DatabaseReference db = FirebaseDatabase.getInstance().getReference();

db.child("Drivers").setValue("drive");

note that push() and setValue() are different. you can read more about saving data on your Realtime Firebase Database here

pastillas
  • 81
  • 6