javaandroidfirebasegradlefirebase-realtime-database

Data is not inserting into Firebase Realtime Database throw Android


I have written simple Firebase Realtime Database code to insert data in it using android java code. Whenever i click on upload button then button is get clicked but firebase code is not executed without showing any error message or warning about data insertion. I have tried to create many different new projects to test the same code but no work.

My code:

In below code none of any method is get invoked from SuccessListener() or FailureListener() but button got clicked.

public void onClick(View v) {
        if(v.getId()==uploadBtn.getId()){ 

    DatabaseReference ref= FirebaseDatabase.getInstance().getReference().child("Users");
                TestModel tm=new TestModel("Angela","angela@123");
                ref.child("Angela").setValue(tm).addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Log.d("app","data inserted");
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.d("app","failed to inserted: "+e.getMessage());
                    }
                });
}

Not getting any error message or warning from above code. And also there is no any change in the firebase database.

My TestModel class:

public class TestModel {
    public String name,passwd;

    public TestModel(String name, String passwd) {
        this.name = name;
        this.passwd = passwd;
    }
}

Rules:

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

Firebase Node Structure:

first-app-default-rtdb:null

build.gradle (Project):

buildscript {
    
    repositories {
        google()
        jcenter()
        
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.2'
        classpath 'com.google.gms:google-services:4.3.10'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle (app):

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

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.simpleapp"
        minSdkVersion 26
        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'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.firebase:firebase-database:20.0.1'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

I have tried below but same issue:

Please help me.


Solution

  • The problem was in Internet connection of the Emulator. Device was offline. Because I have used my Phone's hotspot connection for the emulator. When hotspot was turned off and emulator started then emulator always stays in a offline mode until we restart it.

    Information log or warning, error messages are expected for this problem from Firebase or Android Studio.