将EditText字符串与Firebase数据库的子值进行比较 [英] Comparing EditText string with firebase database's child value

查看:78
本文介绍了将EditText字符串与Firebase数据库的子值进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有一个注册页面,其中只有那些输入正确密码的用户才被允许注册.此密码存储在Firebase数据库中一个名为"Councilpasscodes"的子级中.输入密码后,用户应按提交按钮,在比较两个密码后,如果密码相同,则应允许用户进行注册.这是用于比较编辑文本字符串和子值的代码:

  mpasscode = FirebaseDatabase.getInstance().getReference().child("Councilpasscodes");Submit.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View view){最终字符串passcode1 = editText.getText().toString().trim();mpasscode.addListenerForSingleValueEvent(new ValueEventListener(){@Override公共无效onDataChange(DataSnapshot dataSnapshot){字符串passc =(String)dataSnapshot.child("passcode").getValue();如果(passcode1.equals(passc)){//做一点事}其他if(!passcode1.equals(passc)){//做一点事}}@Override公共无效onCancelled(DatabaseError databaseError){}});}}); 

问题是按下提交按钮后,它并没有真正检查两个字符串,即 if(passcode1.equals(passc)) if(!passcode1.equals(passc))即使输入正确的密码也不会被调用.我不确定代码是什么问题,因为我是应用程序开发的新手.感谢您的帮助.

我认为它根本不执行 addListenerForSingleValue .我不确定为什么会这样.

解决方案

好的伙伴.未调用您的 ValueEventListener onDataChange 的原因是,您无权编辑或读取数据库.发生这种情况是因为Firebase实时数据库的默认安全设置不允许未经身份验证的用户进行任何读取或写入.为了证明这一点,请在Android Monitor中观察您的 warning 日志.(蓝色的.)单击提交后,它将显示捕获的异常,提及Firebase拒绝的权限.

从.

这仍然是一个安全漏洞,因此要在您的应用中设置正确的身份验证,请按照以下步骤操作. https://firebase.google.com/docs/auth/android/start/

此外,像这样将toString()添加到 dataSnapshot.child("passcode").getValue(),以将其转换为String.

字符串passc = dataSnapshot.child("passcode").getValue().toString();

此外,请确保.如果尚未添加此行 FirebaseApp.initializeApp(this); 在你之上 mpasscode = FirebaseDatabase.getInstance().getReference().child("Councilpasscodes");

希望有帮助!

My app has a registration page wherein only those users who enter the right passcode are allowed to register. This passcode is stored inside firebase database inside a child called "Councilpasscodes". After entering the passcode the user should press the submit button, after comparing the two passcodes the user should be allowed to register if the passcodes are same. This is the code for comparing the edit text string and the child value:

mpasscode=FirebaseDatabase.getInstance().getReference().child("Councilpasscodes"); 
submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                final String passcode1=editText.getText().toString().trim();
                mpasscode.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        String passc= (String) dataSnapshot.child("passcode").getValue();
                        if (passcode1.equals(passc)){
                            //do something


                        }else if(!passcode1.equals(passc)){
                            //do something

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

                    }
                });
            }
        });

The problem is after pressing the submit button it is not really checking the two strings i.e. if (passcode1.equals(passc)) and if(!passcode1.equals(passc)) is not being called even after entering the right passcode. I am not sure what is wrong with the code as I am new to app development.Any help is appreciated.Thank you.

EDIT: I think it is not executing addListenerForSingleValue at all. I am not sure why is this happening.

解决方案

Alright mate. The reason why your ValueEventListener's onDataChange isn't getting called is that you're not authorized to edit or read your database. This happens because the default security settings for a firebase real-time database disallows any read or write without an authenticated user. To prove this observe your warning logs inside Android Monitor. (The blue ones.) It will show a caught exception mentioning permission denied from Firebase as soon as you click submit.

Read about Firebase security from here.

For a quick solution what you can do is, you can allow read or writes without any sort of authentication. To do that edit your Realtime database rules and add this instead of what's written there.

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

I'm also gonna attach a screenshot for reference. You'll find this screen inside the firebase console. Inside the console click on your project and then on Database to the left as shown.

.

This is a security lapse none the less, so to set up proper Authentication from your app, follow these steps. https://firebase.google.com/docs/auth/android/start/

Also, add a toString() to dataSnapshot.child("passcode").getValue() like this to convert it to String as it's probably long.

String passc = dataSnapshot.child("passcode").getValue().toString();

Also, just to be sure. Add this line if you haven't already FirebaseApp.initializeApp(this); above your mpasscode=FirebaseDatabase.getInstance().getReference().child("Councilpasscodes");

Hope it helps!

这篇关于将EditText字符串与Firebase数据库的子值进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆