简体   繁体   中英

Adding Data to firebase Realtime Database showing Error

I am making an app based on firebase Realtime database. I have set rules as given below:

{
// Allow anyone to read data, but only authenticated content owners can
// make changes to their data

  "rules": {
    "Users": {
      "$uid": {
        ".read": true,
        // or ".read": "auth.uid != null" for only authenticated users
        ".write": "auth.uid == $uid"
      }
    }
  }
}

I am trying to add data by the following code in java. But getting Error.

myfire = FirebaseDatabase.getInstance();
        myRef = myfire.getReference("Users").child("Some Authentic User");
        //======================

        btnAdmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                stName = etName.getText().toString();
                stRoll = etRoll.getText().toString();


                etName.setText("");
                etRoll.setText("");


                myRef = myfire.getReference();
                myRef.child("201").addValueEventListener(new ValueEventListener() {

                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                       model model =new model();
                       model.setFb01name(stName);
                       model.setFb04roll(stRoll);
                        myRef.child("Basic").setValue(model);
                       Toast.makeText(getApplicationContext                       (),"Sorry",Toast.LENGTH_SHORT).show();
                    }




                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {
                         Toast.makeText(getApplicationContext                       (),"Error",Toast.LENGTH_SHORT).show();
                    }



                });
            }
        });

My model class goes like this:

public class model {

        String fb01name;
        String fb04roll;

        public model() {

        }

        public model(String fb01name, String fb04roll) {
                this.fb01name = fb01name;
                this.fb04roll = fb04roll;
        }



        public String getFb01name() {
                return fb01name;
        }

        public void setFb01name(String fb01name) {
                this.fb01name = fb01name;
        }

        public String getFb04roll() {
                return fb04roll;
        }

        public void setFb04roll(String fb04roll) {
                this.fb04roll = fb04roll;
        }
}

I cannot find the error. The logcat is blank. I was successful in adding data previously.But after changing the rules it failed.I want the database path like this:

(Main Node)Users--

 (first Child)---Authenitic User(As added by "Add User")
     (second child)-----some id( like '201')
           (Targeted Children) 1 ------fb01name (and its value)
            2------fb04roll (and its value)

Can anybody will please guide me practically?

Since onCancelled is called on your listener, that means that you don't have permission to read the data that you're trying to access. If you log the databaseError.toException() that you get in onCancelled you should also see that, as it should tell you that permission was denied.

Distilling your code down, you're attaching a listener to:

myRef = myfire.getReference();
myRef.child("201").addValueEventListener(...

So that is the path /201 in the database, where indeed your rules don't grant anyone read access.

My best guess is that the myRef = myfire.getReference(); line is a mistake, and removing it will lead to reading /Users/Some Authentic User/201 , which is probably what you want to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM