【问题标题】:Data from the instagram clone android application are not reflecting at the realtime database in Firebase来自 instagram 克隆 android 应用程序的数据未反映在 Firebase 中的实时数据库中
【发布时间】:2023-04-06 03:38:01
【问题描述】:

我正在尝试在 Android Studio 中构建 Instagram 应用程序的克隆。因此,我想将克隆应用程序中的所有用户数据存储到 firebase 中的实时数据库中。我还在 Firebase 中创建了一个实时数据库(测试数据库),并在我的应用程序中设置了 FirebaseDatabase 依赖项。问题是当我从注册页面输入数据并单击注册按钮时,它完美地验证了 Firebase 中的数据,但实时数据库上没有任何反映。任何建议对我来说都是完美的。下面,我将针对这个问题分享我的代码:

RegistrationActivity.java

package com.shankhadeep.firebaseinstagram;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.firestore.FirebaseFirestore;

import java.util.HashMap;

public class Registration extends AppCompatActivity {

    private static final String TAG = "TAG";
    EditText username, fullname, email, password;
    Button btn_register;
    TextView txt_login;
    DatabaseReference mRootRef;
    FirebaseAuth auth;
//    FirebaseFirestore fStore;


    ProgressDialog pd;

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

        username = findViewById(R.id.username);
        fullname = findViewById(R.id.fullname);
        email = findViewById(R.id.email);
        password = findViewById(R.id.password);
        btn_register = findViewById(R.id.btn_register);
        txt_login = findViewById(R.id.txt_login);
        pd = new ProgressDialog(this);

        mRootRef = FirebaseDatabase.getInstance().getReference();
        auth = FirebaseAuth.getInstance();
//        fStore = FirebaseFirestore.getInstance();


        txt_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Registration.this,Login.class));
            }
        });

        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String textUserName = username.getText().toString();
                String textFullName = fullname.getText().toString();
                String emailId = email.getText().toString();
                String txt_Password = password.getText().toString();


                if (TextUtils.isEmpty(textUserName) || TextUtils.isEmpty(textFullName) || TextUtils.isEmpty(emailId) || TextUtils.isEmpty(txt_Password)){
                    Toast.makeText(Registration.this,"Empty credentials", Toast.LENGTH_LONG).show();

                }
                else if(txt_Password.length() < 6) {
                    Toast.makeText(Registration.this, "Password too short!", Toast.LENGTH_LONG).show();

                }
                else {
                    registerUser(textUserName, textFullName, emailId, txt_Password);
                    startActivity(new Intent(Registration.this,Login.class));
                    finish();
                }

            }
        });
    }

    private void registerUser(final String textUserName, final String textFullName, final String emailId, final String txt_password) {
        pd.setMessage("Please Wait!");
        pd.show();

        auth.createUserWithEmailAndPassword(emailId , txt_password).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
            @Override
            public void onSuccess(AuthResult authResult) {

                HashMap<String , Object> map = new HashMap<>();
                map.put("name" , textUserName);
                map.put("email", emailId);
                map.put("username" , username);
                map.put("id" , auth.getCurrentUser().getUid());
                map.put("bio" , "");
                map.put("imageurl" , "default");

                mRootRef.child("Users").child(auth.getCurrentUser().getUid()).setValue(map).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()){
                            pd.dismiss();
                            Toast.makeText(Registration.this, "Update the profile " +
                                    "for better expereince", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(Registration.this , MainActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
                            finish();
                        }
                    }
                });

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

    }

}

build.gradle(应用级别)

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"
    defaultConfig {
        applicationId "com.shankhadeep.firebaseinstagram"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.annotation:annotation:1.0.2'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
    implementation 'com.google.firebase:firebase-auth:16.0.5'
    implementation 'com.google.firebase:firebase-database:16.0.4'
    implementation 'com.google.firebase:firebase-storage:16.0.4'
    implementation 'com.google.firebase:firebase-functions:16.1.3'
    implementation 'com.google.firebase:firebase-firestore:17.1.2'
    implementation 'de.hdodenhof:circleimageview:3.1.0'
    implementation 'com.squareup.picasso:picasso:2.71828'

    implementation "com.hendraanggrian.appcompat:socialview:0.2"
    implementation "com.hendraanggrian.appcompat:socialview-commons:0.2"
    implementation 'com.theartofdev.edmodo:android-image-cropper:2.8.0'

    implementation 'com.rengwuxian.materialedittext:library:2.1.4'

    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

这里有一些快照

Database creation and rules setup

Registration page in app

Data entered and registering

Autthentication successfull

Empty realtime database

【问题讨论】:

  • 你的onComplete() 被触发了吗?
  • 是的!它正在打印 Toast 通知。
  • 所以你Toast.makeText(Registration.this, "Update the profile " + "for better expereince", Toast.LENGTH_SHORT).show(); 有效,对吧?您确定您正在查看正确的项目吗?
  • 不,对不起。吐司没有出现。注册后,它直接将我带到仪表板而无需手动登录,因为我在开始活动中添加了 onStart() 方法,该方法将自动验证我的信息并将我重定向到仪表板。但是是的,注册后没有显示Toast功能。我为我上次的错误道歉。

标签: android firebase firebase-realtime-database


【解决方案1】:

如果用户退出,首先获取用户详细信息,否则您应该更新,如果用户退出,则根据用户 ID 更新该用户信息

【讨论】:

    猜你喜欢
    • 2023-01-11
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多