简体   繁体   English

尝试从数据库读取时,Firebase权限被拒绝,但我可以写入(Android)

[英]Firebase permission denied while trying to read from db but I can write(Android)

Edit: I updated retrieveData(). 编辑:我更新了retrieveData()。 Now my method displays the correct amount of elements in my db in onChildAdded() but gets an error while I try to assign Info post to postSnapshot.getValue(Info.class). 现在,我的方法在onChildAdded()中的数据库中显示了正确数量的元素,但是在尝试将Info post分配给postSnapshot.getValue(Info.class)时出现错误。 Here is my Info class without setters,getters etc 这是我的Info类,没有设置器,获取器等

  public Info(String question,String answer,String a,String b,String c,String d){
        this.question=question;
        this.a=a;
        this.b=b;
        this.c=c;
        this.d=d;
        this.answer=answer;
    }
    public Info(){

    }
}





public void retrieveData(){
                Log.e("burdayım sa","saaaaaaaaaaa");
                databaseReference.addChildEventListener(new ChildEventListener() {
                    @Override
                    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                        Log.e("Count " ,""+dataSnapshot.getChildrenCount());
                        for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                            Info post = postSnapshot.getValue(Info.class);
                            Log.e("Get Data", post.getQuestion());
                        }
                    }

                    @Override
                    public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                        Log.e("Count " ,""+dataSnapshot.getChildrenCount());
                        for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                            Info post = postSnapshot.getValue(Info.class);
                            Log.e("Get Data", post.getQuestion());
                        }
                    }

                    @Override
                    public void onChildRemoved(DataSnapshot dataSnapshot) {

                    }

                    @Override
                    public void onChildMoved(DataSnapshot dataSnapshot, String s) {

                    }

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

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

This is the error : com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.sahilliolu.rqagame.Info 这是错误:com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为com.example.sahilliolu.rqagame.Info类型


So, this is my code to receive the data. 因此,这是我接收数据的代码。 I handle the authentication in the previous activity and I can write things to my DB from this activity( I do not use firebase object for that) but when I try to get an object it gives me permission denied. 我在上一个活动中处理了身份验证,并且可以从该活动中向我的数据库写入内容(我不为此使用firebase对象),但是当我尝试获取一个对象时,它会给我拒绝权限。 How can I solve this? 我该如何解决? Here is how I write to db. 这是我写数据库的方法。

 firebaseAuth=FirebaseAuth.getInstance();
        if(firebaseAuth.getCurrentUser()==null){
            finish();
            startActivity(new Intent(this,LoginActivity.class));
        }
        FirebaseUser user=firebaseAuth.getCurrentUser();
        databaseReference= FirebaseDatabase.getInstance().getReference().child("questions");



Info info=new Info(question,answer,a,b,c,d);
                newDatabaseReference=databaseReference.push();
                newDatabaseReference.setValue(info);
                Toast.makeText(getApplicationContext(),"Your question is created",Toast.LENGTH_SHORT).show();

You are using both the legacy SDK and the new SDK: 您正在使用旧版SDK和新版SDK:

// legacy
firebase.setAndroidContext(this);
firebase=new Firebase("https://mylink");

// new
FirebaseUser user=firebaseAuth.getCurrentUser();
databaseReference= FirebaseDatabase.getInstance().getReference().child("questions");

This is not good practice and is likely the source of the problem. 这不是一个好习惯,很可能是问题的根源。

You should use the new SDK exclusively. 您应该专门使用新的SDK。 Remove this line from your build dependencies and update your code: 从构建依赖项中删除此行并更新代码:

compile 'com.firebase:firebase-client-android:2.X.X'

The Upgrade Guide is helpful. 升级指南很有帮助。

您指的是错误的节点,该节点不包含所需的变量,而仅包含一个字符串(可能只是问题)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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