Firebase onDataChange not firing

时间:2017-05-16 09:16:46

标签: java android firebase firebase-realtime-database

I am trying to retrieve some data from Firebase. Here is my event listener that never gets fired, neither onCancelled method. I have tried the answer from here. I've checked the rules. I have reading permissions. I added some wait time

public class LoginActivity extends AppCompatActivity {

private DatabaseReference databaseReference;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_login);


     Button btn = (Button)findViewById(R.id.buttonSign);

     TextView txt = (TextView)findViewById(R.id.textView);

     Button btn2 = (Button) findViewById(R.id.button2);

     FirebaseDatabase database = FirebaseDatabase.getInstance();
     databaseReference = database.getReference();

     txt.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             createNewDbListener();
            Intent intent1 = new Intent(getApplicationContext(), MainActivity.class);
             startActivity(intent1);
         }
     });

     btn.setOnClickListener(new View.OnClickListener(){
         @Override
         public void onClick(View v) {
             Intent intentLoginP = new Intent(getApplicationContext(), LoginPageActivity.class);
             startActivity(intentLoginP);
         }
     });

     btn2.setOnClickListener(new View.OnClickListener(){
         @Override
         public void onClick(View v) {
             Intent intentSignUp = new Intent(getApplicationContext(), SignUpActivity.class);
             startActivity(intentSignUp);
         }
     });
 } 

private void createNewDbListener() {
    Log.d(TAG, String.valueOf(databaseReference.child("counties")));

    databaseReference.child("counties").addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Log.d("onDataChange", String.valueOf(databaseReference.child("counties")));
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
             Log.e("DEBUG","OnCancelled", databaseError.toException());
        }
    });    } }

I've have done my research and I've tried almost everything. I do not know what the problem is. My function createNewDbListener() is entered and the first log gets fired.

EDIT: The log from my createNewDbListener function:

05-16 12:25:18.159 7153-7153/com.bla.dipproject D/DEBUG: https://dipproject-3953a.firebaseio.com/counties

2 个答案:

答案 0 :(得分:0)

public void getAllUsersFromFirebase() {
    DatabaseReference UserRef = FirebaseDatabase.getInstance().getReference().child(Constant.FIREBASE_USERS);

    UserRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Iterator<DataSnapshot> dataSnapshots = dataSnapshot.getChildren().iterator();
            List<FcmUser> fcmUsers = new ArrayList<>();
            while (dataSnapshots.hasNext()) {
                DataSnapshot dataSnapshotChild = dataSnapshots.next();
                FcmUser fcmUser = dataSnapshotChild.getValue(FcmUser.class);

                 Log.e("Fcm User Name",fcmUser.name);
            }
            mOnGetAllUsersListener.onGetAllUsersSuccess(fcmUsers);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            mOnGetAllUsersListener.onGetAllUsersFailure(databaseError.getMessage());
        }
    });
}

尝试上面的示例代码并对意图发表评论,直到没有得到任何调试响应。

答案 1 :(得分:0)

我遇到了完全相同的问题。尝试了一切。原来我在控制台中配置了错误的访问权限。转到firebase控制台并确保您的应用程序配置正确(设置 - &gt;项目设置 - >添加应用),并将google-services.json放在正确的位置。