简体   繁体   中英

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

I am trying to build a clone of Instagram application in Android Studio. So, I want to store all the data of users from the clone application to the realtime database in firebase. I have also created one realtime database(Test database) in Firebase and set the FirebaseDatabase dependency in my application. The problem is when I'm entering the data from my registration page and click on the register button, it was perfectly authenticating the data in Firebase but nothing is reflecting on the realtime database. Any suggestions will be perfect for me. Below, I'm sharing my codes following to this problem:

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(app level)

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'
}

Here are some snapshots

Database creation and rules setup

Registration page in app

Data entered and registering

Autthentication successfull

Empty realtime database

first of all get user details if user is exits if not then you should update and if user is exits then update that one accroding to user id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM