Nikita K
Nikita K

Reputation: 95

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.

Upvotes: 2

Views: 4687

Answers (5)

Steve Vaius
Steve Vaius

Reputation: 1

I had the same issue and now I found that problem was firebase data central setting in google-services.json file. Before I set my data center other than US_Central, I downloaded and put json file in app folder. This causes to not find database file because of different URL for Belgium. I hope this helps to others in case they have similar problems

Upvotes: 0

Nikita K
Nikita K

Reputation: 95

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.

Upvotes: 1

Yogesh Pathania
Yogesh Pathania

Reputation: 51

I facing this issue from last 3 Days finally i found some solution

First thing update your json file

Second check read, Write Rules is True

Third Go to Project Seetting->Open Tab Data Usage-> Now You will see Product Section pls make sure you unenforced the dataBase if you Bymistake enforced that option After this you will finally Read or Write the Data in DataBase

Thanks

Happy Caoding

Here Check Screen Short for unenforced data Like this [1]: https://i.sstatic.net/s0pzI.png

Upvotes: 1

MoonLight
MoonLight

Reputation: 557

Add setter methods in your class and then try again.

Upvotes: 0

Sakhawat Hossain
Sakhawat Hossain

Reputation: 463

You can try with the following possible solutions:

  1. Check service JSON file is placed under the app folder.
  2. Check-in the firebase console and your database reference is valid or not null.

Upvotes: 0

Related Questions