簡體   English   中英

來自 Instagram 克隆 android 應用程序的數據未反映在 Firebase 中的實時數據庫中

[英]Data from the instagram clone android application are not reflecting at the realtime database in Firebase

我正在嘗試在 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'
}

這是一些快照

數據庫創建和規則設置

應用程序中的注冊頁面

數據輸入和注冊

認證成功

清空實時數據庫

首先獲取用戶詳細信息,如果用戶退出,如果沒有,那么您應該更新,如果用戶退出,則根據用戶 ID 更新該用戶詳細信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM