【问题标题】:How to save users email and name after authentication in database (Firebase)?如何在数据库(Firebase)中进行身份验证后保存用户的电子邮件和姓名?
【发布时间】:2017-11-20 04:40:20
【问题描述】:

我是 Android 新手,也是 Firebase 新手。我正在开发一个 Android 应用程序,用户使用电子邮件和密码创建他的帐户。

如何将用户身份(如姓名、地址等)连同电子邮件一起保存到 Firebase 数据库中?

这是我的代码,其中用户是使用电子邮件和密码创建的,以及有关要保存在数据库中的用户的额外信息的代码。

我的应用在创建新用户后崩溃了,而且它没有在数据库中存储信息。

auth.createUserWithEmailAndPassword(emailId, password)
    .addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {

                if (task.isSuccessful()) {

                    String user_id = auth.getCurrentUser().getUid();

                    DatabaseReference current_user_db = reference.child(user_id);

                    current_user_db.child("FNAME").setValue(nameF);

                    current_user_db.child("LNAME").setValue(nameL);

                    current_user_db.child("EMAIL").setValue(email);

                    progress.hide();

                    Intent intent = new Intent(MainActivity.this, ProfileActivity.class);

                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                    startActivity(intent);

                }

        }
    });

这是日志。

06-17 21:01:17.082 2262-2262/com.google.android.googlequicksearchbox:search I/MicroDetectionWorker: #onError(false)
06-17 21:01:22.096 2262-2262/com.google.android.googlequicksearchbox:search I/MicroDetectionWorker: Micro detection mode: [mDetectionMode: [1]].
06-17 21:01:22.097 2262-2262/com.google.android.googlequicksearchbox:search I/AudioController: Using mInputStreamFactoryBuilder
06-17 21:01:22.103 2262-4023/com.google.android.googlequicksearchbox:search I/MicroRecognitionRunner: Starting detection.
06-17 21:01:22.112 2262-3576/com.google.android.googlequicksearchbox:search I/MicrophoneInputStream: mic_starting com.google.android.apps.gsa.staticplugins.z.c@4a20126
06-17 21:01:22.112 1304-1304/? W/APM_AudioPolicyManager: getInputForAttr() failed opening input: samplingRate 16000, format 1, channelMask 10
06-17 21:01:22.112 2262-3576/com.google.android.googlequicksearchbox:search E/AudioRecord: Could not get audio input for session 857, record source 1999, sample rate 16000, format 0x1, channel mask 0x10, flags 0
06-17 21:01:22.114 2262-3576/com.google.android.googlequicksearchbox:search E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -22.
06-17 21:01:22.114 2262-3576/com.google.android.googlequicksearchbox:search E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
06-17 21:01:22.114 2262-3576/com.google.android.googlequicksearchbox:search I/MicrophoneInputStream: mic_started com.google.android.apps.gsa.staticplugins.z.c@4a20126
06-17 21:01:22.115 2262-3576/com.google.android.googlequicksearchbox:search E/ActivityThread: Failed to find provider info for com.google.android.apps.gsa.testing.ui.audio.recorded
06-17 21:01:22.116 2262-2262/com.google.android.googlequicksearchbox:search I/MicroDetectionWorker: onReady
06-17 21:01:22.130 2262-3576/com.google.android.googlequicksearchbox:search I/MicrophoneInputStream: mic_close com.google.android.apps.gsa.staticplugins.z.c@4a20126
06-17 21:01:22.130 2262-4023/com.google.android.googlequicksearchbox:search I/MicroRecognitionRunner: Detection finished
06-17 21:01:22.130 2262-4023/com.google.android.googlequicksearchbox:search W/ErrorReporter: reportError [type: 211, code: 524300]: Error reading from input stream
06-17 21:01:22.131 2262-2432/com.google.android.googlequicksearchbox:search I/MicroRecognitionRunner: Stopping hotword detection.
06-17 21:01:22.131 2262-4023/com.google.android.googlequicksearchbox:search W/ErrorProcessor: onFatalError, processing error from engine(4)
com.google.android.apps.gsa.shared.speech.a.g: Error reading from input stream
  at com.google.android.apps.gsa.staticplugins.recognizer.i.a.a(SourceFile:342)
  at com.google.android.apps.gsa.staticplugins.recognizer.i.a$1.run(SourceFile:1367)
  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  at com.google.android.apps.gsa.shared.util.concurrent.a.ak.run(SourceFile:66)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
  at java.lang.Thread.run(Thread.java:761)
  at com.google.android.apps.gsa.shared.util.concurrent.a.ad$1.run(SourceFile:85)
Caused by: com.google.android.apps.gsa.shared.exception.GsaIOException: Error code: 393238 | Buffer overflow, no available space.
  at com.google.android.apps.gsa.speech.audio.Tee.g(SourceFile:2531)
  at com.google.android.apps.gsa.speech.audio.ap.read(SourceFile:555)
  at java.io.InputStream.read(InputStream.java:101)
  at com.google.android.apps.gsa.speech.audio.al.run(SourceFile:362)
  at com.google.android.apps.gsa.speech.audio.ak$1.run(SourceFile:471)
  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  at com.google.android.apps.gsa.shared.util.concurrent.a.ak.run(SourceFile:66)
  at com.google.android.apps.gsa.shared.util.concurrent.a.ax.run(SourceFile:139)
  at com.google.android.apps.gsa.shared.util.concurrent.a.ax.run(SourceFile:139)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
  at java.lang.Thread.run(Thread.java:761) 
  at com.google.android.apps.gsa.shared.util.concurrent.a.ad$1.run(SourceFile:85) 
06-17 21:01:22.131 2262-4023/com.google.android.googlequicksearchbox:search I/AudioController: internalShutdown
06-17 21:01:22.133 2262-2262/com.google.android.googlequicksearchbox:search I/MicroDetector: Keeping mic open: false
06-17 21:01:22.133 2262-2262/com.google.android.googlequicksearchbox:search I/MicroDetectionWorker: #onError(false)
06-17 21:01:22.133 2262-4022/com.google.android.googlequicksearchbox:search I/DeviceStateChecker: DeviceStateChecker cancelled

【问题讨论】:

  • 是的,所以你的问题是什么。存储您对电子邮件、姓名等所做的剩余详细信息。
  • @DushyantSuthar 这次我稍微更改了代码,现在我的应用程序崩溃了
  • 可以发日志吗?
  • @Asama 感谢您的回复,我已经在代码下方发布了日志
  • @KaushalTopinkatti 在您发布的日志中,没有关于 Firebase 的参考。

标签: android firebase


【解决方案1】:

尝试使用 HashMap(个人认为我觉得它更具可读性)这是我更新用户设置的代码,整个函数通过一个按钮被调用,希望这会有所帮助。

private void updatesettings() {
    String UserName = username.getText().toString().toLowerCase().trim();
    String PhoneNumber= pno.getText().toString();

    FirebaseUser user = mauth.getCurrentUser();
    String uid = user.getUid();

    //this is to enter all the details under the UID of currently logged in user

    dbr = FirebaseDatabase.getInstance().getReference().child("Users").child(uid);

    HashMap<String, String> userMap = new HashMap<String, String>();
    userMap.put("name", UserName);
    userMap.put("phone number", PhoneNumber);
    userMap.put("status", "jdjdjjdjdjdj");
    userMap.put("image", "default");
    userMap.put("thumbnail", "default");

    dbr.setValue(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            if(task.isSuccessful()){
                // Toast.makeText(UpdateUserSettings.this, "Your settings have been updated", Toast.LENGTH_LONG).show();
                startActivity(new Intent(UpdateUserSettings.this, MainActivity.class));
            }
            else{
                Toast.makeText(UpdateUserSettings.this, task.getException().getMessage(), Toast.LENGTH_LONG).show();
            }
        }
    });
}

【讨论】:

    猜你喜欢
    • 2018-04-01
    • 2016-12-13
    • 1970-01-01
    • 2020-04-28
    • 2017-10-26
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多