【问题标题】:Unable to fetch data from Firebase (Android)无法从 Firebase (Android) 获取数据
【发布时间】:2020-04-25 19:10:13
【问题描述】:

我正在尝试从 google firebase 获取数据,但每次我都尝试获取数据。 “其他”案例每次都在运行。试图改变 if(dataSnapshot.exists()) 但得到同样的错误。请帮忙解决。

public void retrieveProfileInfo() {
    currentUserID = mAuth.getUid();
    key = rootRef.child("profiles").push().getKey();
    rootRef.child("users-profiles").child(currentUserID).child(key)
            .addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    try {
                        if ((dataSnapshot.child("name").getValue()) != null){
                            String retrieveName = dataSnapshot.child("name").getValue().toString();
                            String retrieveStatus = dataSnapshot.child("about").getValue().toString();
                            nameBox.setText(retrieveName);
                            aboutBox.setText(retrieveStatus);
                        } else {
                            Toast.makeText(UserProfileActivity.this, currentUserID+"else - retrieveProfileInfo()",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }catch (Exception e){
                        Log.i("retrieveProfileInfo : ", "error is : " + e);
                        Toast.makeText(UserProfileActivity.this, e+" : Error",
                                Toast.LENGTH_LONG).show();
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });

}

【问题讨论】:

    标签: java android firebase firebase-realtime-database


    【解决方案1】:

    你不能按下随机键并从中读取,你需要循环遍历users-profile/userID下的孩子:

    public void retrieveProfileInfo() {
    
    //current user ID
    currentUserID = mAuth.getUid();
    
    ValueEventListener listener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
    
          for(DataSnapshot ds:dataSnapshot.getChildren()){
    
           //get the values
           String name = ds.child("name").getValue(String.class);
           String about = ds.child("about").getValue(String.class);
           String uid = ds.child("uid").getValue(String.class);
    
    
          }       
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {
            // log error
            Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
    
        }
    };
    rootRef.child("users-profiles").child(currentUserID).addValueEventListener(listener);
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-10
      • 1970-01-01
      • 2022-09-23
      • 2021-10-25
      • 1970-01-01
      相关资源
      最近更新 更多