【问题标题】:Insert data to Realtime Database does not works将数据插入实时数据库不起作用
【发布时间】:2021-03-30 00:43:15
【问题描述】:

我正在尝试创建一个应用程序,如果有人想使用我的应用程序,他们必须创建一个用户。 当他们注册时,他们将插入名字、姓氏、电话和电子邮件地址。 在我使用 Firebase 授权确认手机是他们的之后,我想将他的数据插入到我的实时数据库中名为 Users 的表中。

当我尝试使用此功能时:

**编辑:我添加了 onComplete 函数,但它没有执行 **

private void storeNewUserData() {
        FirebaseDatabase rootNode = FirebaseDatabase.getInstance();
        DatabaseReference reference = rootNode.getReference("Users");
        User user = new User(phone,firstName,lastName,email);
        reference.setValue(user).addOnCompleteListener(this);
    }


@Override
public void onComplete(@NonNull Task<Void> task) {
    if(task.isSuccessful()){
        Toast.makeText(this, "Inserted User: "+phone+firstName+lastName+email, Toast.LENGTH_SHORT).show();
    }
    else{
        Toast.makeText(this, task.getException().toString(), Toast.LENGTH_SHORT).show();
    }
}

数据库尚未更新,但出现了 Toast(包含所有数据)。

我认为我的代码没有问题,也许是我的 Firebase 帐户有问题。无论如何,我将规则添加到此数据库和表格图像中。

规则

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

build.gradle(app)

dependencies {
    implementation platform('com.google.firebase:firebase-bom:26.8.0')
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-auth:20.0.3'
    implementation 'com.google.firebase:firebase-database:19.7.0'

    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation "androidx.browser:browser:1.3.0"
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

    testImplementation 'junit:junit:4.13.2'

}

';

private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
    mAuth.signInWithCredential(credential).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
        @Override
        public void onSuccess(AuthResult authResult) {
            progressDialog.dismiss();
            User user = new User(phone,firstName,lastName,email);
            storeNewUserData();
            Toast.makeText(PhoneAuthActivity.this, "Logged in as "+phone, Toast.LENGTH_SHORT).show();
            startActivity(new Intent(PhoneAuthActivity.this, MainActivity.class));
        }


    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            progressDialog.dismiss();
            Toast.makeText(PhoneAuthActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
}

logcat

2021-03-30 03:41:02.326 15207-15345/com.example.teorialearning E/FirebaseAuth: [GetAuthDomainTask] Error getting project config. Failed with INVALID_CERT_HASH 400
2021-03-30 03:41:02.344 15207-15257/com.example.teorialearning V/FA: Screen exposed for less than 1000 ms. Event not sent. time: 396
2021-03-30 03:41:02.345 15207-15257/com.example.teorialearning V/FA: Activity paused, time: 15824705
2021-03-30 03:41:02.389 15207-15207/com.example.teorialearning E/zzf: Failed to get reCAPTCHA token with error [There was an error while trying to get your package certificate hash.]- calling backend without app verification
2021-03-30 03:41:02.391 15207-15257/com.example.teorialearning V/FA: Activity resumed, time: 15824751
2021-03-30 03:41:02.395 15207-15245/com.example.teorialearning W/System: Ignoring header X-Firebase-Locale because its value was null.
2021-03-30 03:41:02.641 15207-15207/com.example.teorialearning D/MAIN_TAG: onCodeSent:ALiwoWIdFZrl9ResXiUM94aPJW-csmRjdDOqoa9DTOkNQbv2Vf8YN7X6mvBwXgg9MJxEnFUVoE2rWAP2JzpT8QdBcRK2kIi82e_uO3pXW_rC5CZqWgJaqYqPO6nMOxiavmaJELnkSRtTXo6HoYoEY2_CmtVplEbz2Q
2021-03-30 03:41:02.644 15207-15333/com.example.teorialearning D/EGL_emulation: eglMakeCurrent: 0xae4856c0: ver 3 1 (tinfo 0xae483990)
2021-03-30 03:41:02.677 15207-15333/com.example.teorialearning D/EGL_emulation: eglMakeCurrent: 0xae4856c0: ver 3 1 (tinfo 0xae483990)
2021-03-30 03:41:02.739 15207-15333/com.example.teorialearning D/EGL_emulation: eglMakeCurrent: 0xae4856c0: ver 3 1 (tinfo 0xae483990)
2021-03-30 03:41:04.679 15207-15333/com.example.teorialearning D/EGL_emulation: eglMakeCurrent: 0xae4856c0: ver 3 1 (tinfo 0xae483990)
2021-03-30 03:41:04.789 15207-15333/com.example.teorialearning D/EGL_emulation: eglMakeCurrent: 0xae4856c0: ver 3 1 (tinfo 0xae483990)
2021-03-30 03:41:07.416 15207-15257/com.example.teorialearning V/FA: Inactivity, disconnecting from the service
2021-03-30 03:41:08.627 15207-15245/com.example.teorialearning W/System: Ignoring header X-Firebase-Locale because its value was null.
2021-03-30 03:41:08.668 15207-15333/com.example.teorialearning D/EGL_emulation: eglMakeCurrent: 0xae4856c0: ver 3 1 (tinfo 0xae483990)
2021-03-30 03:41:08.685 15207-15333/com.example.teorialearning D/EGL_emulation: eglMakeCurrent: 0xae4856c0: ver 3 1 (tinfo 0xae483990)
2021-03-30 03:41:08.691 15207-15333/com.example.teorialearning D/EGL_emulation: eglMakeCurrent: 0xae4856c0: ver 3 1 (tinfo 0xae483990)
2021-03-30 03:41:08.696 15207-15333/com.example.teorialearning D/EGL_emulation: eglMakeCurrent: 0xae4856c0: ver 3 1 (tinfo 0xae483990)
2021-03-30 03:41:08.755 15207-15333/com.example.teorialearning D/EGL_emulation: eglMakeCurrent: 0xae4856c0: ver 3 1 (tinfo 0xae483990)
2021-03-30 03:41:08.936 15207-15245/com.example.teorialearning W/System: Ignoring header X-Firebase-Locale because its value was null.
2021-03-30 03:41:09.147 15207-15245/com.example.teorialearning D/FirebaseAuth: Notifying id token listeners about user ( QgYB9guxYgT2AQbr8u9CVrGnELw2 ).
2021-03-30 03:41:09.149 15207-15333/com.example.teorialearning D/EGL_emulation: eglMakeCurrent: 0xae4856c0: ver 3 1 (tinfo 0xae483990)
2021-03-30 03:41:09.178 15207-15257/com.example.teorialearning V/FA: Recording user engagement, ms: 6787
2021-03-30 03:41:09.180 15207-15257/com.example.teorialearning V/FA: Connecting to remote service
2021-03-30 03:41:09.181 15207-15257/com.example.teorialearning V/FA: Activity paused, time: 15831539
2021-03-30 03:41:09.189 15207-15333/com.example.teorialearning D/EGL_emulation: eglMakeCurrent: 0xae4856c0: ver 3 1 (tinfo 0xae483990)
2021-03-30 03:41:09.193 15207-15257/com.example.teorialearning V/FA: Connection attempt already in progress
2021-03-30 03:41:09.212 15207-15225/com.example.teorialearning V/FA: onActivityCreated
2021-03-30 03:41:09.229 15207-15257/com.example.teorialearning V/FA: Activity resumed, time: 15831590
2021-03-30 03:41:09.233 15207-15333/com.example.teorialearning D/EGL_emulation: eglMakeCurrent: 0xae4856c0: ver 3 1 (tinfo 0xae483990)
2021-03-30 03:41:09.243 15207-15257/com.example.teorialearning V/FA: Connection attempt already in progress
2021-03-30 03:41:09.244 15207-15257/com.example.teorialearning V/FA: Connection attempt already in progress
2021-03-30 03:41:09.295 15207-15257/com.example.teorialearning D/FA: Connected to remote service
2021-03-30 03:41:09.296 15207-15257/com.example.teorialearning V/FA: Processing queued up service tasks: 4
2021-03-30 03:41:09.298 15207-15333/com.example.teorialearning D/EGL_emulation: eglMakeCurrent: 0xae4856c0: ver 3 1 (tinfo 0xae483990)
2021-03-30 03:41:09.329 15207-15207/com.example.teorialearning W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
2021-03-30 03:41:09.330 15207-15207/com.example.teorialearning W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
2021-03-30 03:41:11.206 15207-15333/com.example.teorialearning D/EGL_emulation: eglMakeCurrent: 0xae4856c0: ver 3 1 (tinfo 0xae483990)
2021-03-30 03:41:13.170 15207-15333/com.example.teorialearning D/EGL_emulation: eglMakeCurrent: 0xae4856c0: ver 3 1 (tinfo 0xae483990)
2021-03-30 03:41:14.310 15207-15257/com.example.teorialearning V/FA: Inactivity, disconnecting from the service
2021-03-30 03:42:02.393 15207-15348/com.example.teorialearning W/FirebaseAuth: [SmsRetrieverHelper] Timed out waiting for SMS.

P.S:我用的是安卓工作室

【问题讨论】:

  • 您应该使用ValueEventListener 来跟踪特定节点/分支中的更改。
  • 您是否尝试将完整的侦听器附加到 setValue() 方法?请回复@AlexMamo
  • @AlexMamo 我没试过请附上代码让我试试
  • @ShubhamPanchal 请附上代码,我可以试试
  • reference.setValue(user).addOnCompleteListener(/* ... */);

标签: java android firebase


【解决方案1】:

我可以通过更改设备的互联网连接网络来解决此问题。该应用无法在移动数据上运行,但可以在其他设备的 WIFI 上运行。

问题背后的原因尚不清楚。

参考答案->https://stackoverflow.com/a/51122769/13546426

【讨论】:

    猜你喜欢
    • 2016-07-02
    • 2012-04-09
    • 2017-10-07
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多