【问题标题】:Firebase Realtime Database long values always return 0 even though it is notFirebase 实时数据库长值始终返回 0,即使它不是
【发布时间】:2018-04-02 08:37:21
【问题描述】:

我一直在尝试寻找解决方案,但无济于事。我显然不是 Firebase 专家,并且仍在学习如何编码。

我目前正在创建一个登录系统,在该系统中,除了 Firebase 提供的电子邮件验证系统之外,我还必须自己手动执行另一层身份验证。因此,为了实现这一点,我在我的项目中引入了 Firebase 实时数据库。这是数据库中 JSON 文件的 sn-p。

{
 "users" : {
  "abfkbnqeiurbnafjbnaojn" : {
   "app_authorized" : 1,
   "email" : "example51245@gmail.com",
   "full_name" : "Test Sample",
   "user_id" : "abfkbnqeiurbnafjbnaojn"
  }
 }
}

从 JSON 文件中可以看出,app_authorized 键值对将控制用户是否可以登录(0 表示用户不符合条件,1 表示用户有资格登录)。对于所有键值对,我正在使用来自 Firebase 登录的用户的 UID,以及电子邮件和密码系统的父级。我也知道 app_authorized 的值类型很长。

这是我在 SignInActivity 中使用的代码。

long isAppAuthorized;

//some codes here

private void init() {

        btnSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: attempting to log in");

                String email = emailEditText.getText().toString();
                String password = passwordEditText.getText().toString();

                //validates respective EditTexts
                if (TextUtils.isEmpty(email)) {

                    emailEditText.setError("Please fill in your e-mail address!");

                }
                else if (TextUtils.isEmpty(password)) {

                    passwordEditText.setError("Please fill in your password!");

                } else {

                    progressDialog.show();

                    mAuth.signInWithEmailAndPassword(email, password)
                            .addOnCompleteListener(SignInActivity.this, new OnCompleteListener<AuthResult>() {
                                @Override
                                public void onComplete(@NonNull Task<AuthResult> task) {
                                    Log.d(TAG, "signInWithEmail:onComplete:" + task.isSuccessful());

                                    FirebaseUser user = mAuth.getCurrentUser();
                                    firebaseDatabase = FirebaseDatabase.getInstance();

                                    userID = user.getUid();

                                    final DatabaseReference myRef = firebaseDatabase.getReference("users/" + userID + "/app_authorized");

                                    myRef.addValueEventListener(new ValueEventListener() {
                                        @Override
                                        public void onDataChange(DataSnapshot dataSnapshot) {
                                            //gets value of isAppAuthorized
                                            isAppAuthorized = dataSnapshot.getValue(long.class);
                                        }

                                        @Override
                                        public void onCancelled(DatabaseError databaseError) {

                                        }
                                    });

                                    // If sign in fails, display a message to the user. If sign in succeeds
                                    // the auth state listener will be notified and logic to handle the
                                    // signed in user can be handled in the listener.
                                    if (!task.isSuccessful()) {

                                        Log.w(TAG, "signInWithEmail:failed", task.getException());
                                        Toast.makeText(SignInActivity.this, R.string.auth_failed,
                                                Toast.LENGTH_SHORT).show();
                                        progressDialog.dismiss();

                                    } else {

                                        try {
                                            //this is the part where I check for both isEmailVerified and isAppAuthorized
                                            if(user.isEmailVerified() && isAppAuthorized == 1) {

                                                Log.d(TAG, "onComplete: success. e-mail is verified " + user.isEmailVerified() + " userIsAuthorized: " + isAppAuthorized);
                                                Intent intent = new Intent(SignInActivity.this, HomeActivity.class);
                                                startActivity(intent);

                                            } else {

                                                Log.d(TAG, "onComplete: failed: isEmailVerified: " + user.isEmailVerified() + " userIsAuthorized: " + isAppAuthorized);

                                                progressDialog.dismiss();
                                                mAuth.signOut();
                                                Intent intent = new Intent(SignInActivity.this, MainActivity.class);
                                                startActivity(intent);

                                            }

                                        } catch(NullPointerException e) {

                                            Log.e(TAG, "onComplete: NullPointerException: " + e.getMessage());

                                        }
                                    }

                                    // ...
                                }
                            });
                }
            }
        });

    }

我遇到的问题是无论我将什么值放入数据库,isAppAuthorized long 总是返回 0,从而使我的第二层授权无用。

我希望有人能够帮助我解决这个问题,因为我尝试了很多东西,但仍然无法理解问题的根源。

【问题讨论】:

  • 试试isAppAuthorized = dataSnapshot.getValue(Long.class);
  • 这在我将变量声明和 long.class 都更改为大写 L 的 Long 后解决了(尽管它返回了 NullException 错误)。为什么我必须使用 Long 而不是 long?
  • Long 是对对象的引用,而 long 是原语。

标签: java android firebase firebase-realtime-database firebase-authentication


【解决方案1】:

用途:

isAppAuthorized = dataSnapshot.getValue(Integer.class);

【讨论】:

  • 参考Tarun的评论,我也试过这个。它也有效(但一开始出现 NullException 错误)。这是因为与 Integer 和 Long 相比,int 和 long 不能返回空引用吗?感谢您的帮助,我会尝试解决 NullException 错误。
  • @applejuice dataSnapshot.getValue(Long.class);你需要通过类(Long 是一门课)但 long 不是一门课
  • @applejuice 如果 app_authorized 不存在,您可以获得 null 从 firebase 控制台检查您的数据库
  • 非常感谢您的澄清!
猜你喜欢
  • 2019-03-26
  • 1970-01-01
  • 2021-04-29
  • 2020-01-02
  • 1970-01-01
  • 2020-07-25
  • 1970-01-01
  • 1970-01-01
  • 2017-09-05
相关资源
最近更新 更多