Firebase实时数据库未显示从Android Studio写入的数据。

huangapple go评论79阅读模式
英文:

Firebase Realtime database is not showing data that is being written from android studio

问题

I am having a problem writing into Firebase real-time database. The app is running fine with no build errors. but when I look into the database nothing is changing. I am trying to write into the database. The account which is logged in in Android and Firebase is the same. I have also given the internet permission in the manifest file. I have included the JSON file in the project. I have added all the required dependencies in gradle. the gradle file

package com.example.firebasehometry;

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

import android.os.Bundle;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class MainActivity extends AppCompatActivity {

    FirebaseDatabase database;
    DatabaseReference reference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        database = FirebaseDatabase.getInstance("https://fir-home-53258-default-rtdb.firebaseio.com/");
        reference = database.getReference("student");
        add();
    }

    public void add(){
        Toast.makeText(this, "before adding", Toast.LENGTH_SHORT).show();
        reference.setValue("ashar khan").addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                Toast.makeText(MainActivity.this, "Successfully inserted", Toast.LENGTH_SHORT).show();
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(MainActivity.this, "Error inserting", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
plugins {
    id 'com.android.application'
    id 'com.google.gms.google-services'
}

android {
    namespace 'com.example.firebasehometry'
    compileSdk 33

    defaultConfig {
        applicationId "com.example.firebasehometry"
        minSdk 25
        targetSdk 33
        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 platform('com.google.firebase:firebase-bom:32.0.0')
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.android.material:material:1.9.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'com.google.firebase:firebase-database:20.0.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

You mentioned that you've tried various methods, but the real-time database is still not updating. If you have any specific questions or issues, please provide more details so that I can assist you further.

英文:

I am having a problem writing into Firebase real-time database. The app is running fine with no build errors. but when I look into the database nothing is changing. I am trying to write into the database. The account which is logged in in Android and Firebase is the same. I have also given the internet permission in the manifest file. I have included the JSON file in the project. I have added all the required dependencies in gradle. the gradle file

`

  package com.example.firebasehometry;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class MainActivity extends AppCompatActivity {
FirebaseDatabase database;
DatabaseReference reference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
database = FirebaseDatabase.getInstance(&quot;https://fir-home-53258-default-rtdb.firebaseio.com/&quot;);
reference = database.getReference(&quot;student&quot;);
add();
}
public void add(){
Toast.makeText(this, &quot;before adding&quot;, Toast.LENGTH_SHORT).show();
reference.setValue(&quot;ashar khan&quot;).addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
@Override
public void onComplete(@NonNull Task&lt;Void&gt; task) {
Toast.makeText(MainActivity.this, &quot;Successfully inserted&quot;, Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, &quot;Error inserting&quot;, Toast.LENGTH_SHORT).show();
}
});
}
}
plugins {
id &#39;com.android.application&#39;
id &#39;com.google.gms.google-services&#39;
}
android {
namespace &#39;com.example.firebasehometry&#39;
compileSdk 33
defaultConfig {
applicationId &quot;com.example.firebasehometry&quot;
minSdk 25
targetSdk 33
versionCode 1
versionName &quot;1.0&quot;
testInstrumentationRunner &quot;androidx.test.runner.AndroidJUnitRunner&quot;
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(&#39;proguard-android-optimize.txt&#39;), &#39;proguard-rules.pro&#39;
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation platform(&#39;com.google.firebase:firebase-bom:32.0.0&#39;)
implementation &#39;androidx.appcompat:appcompat:1.6.1&#39;
implementation &#39;com.google.firebase:firebase-analytics&#39;
implementation &#39;com.google.android.material:material:1.9.0&#39;
implementation &#39;androidx.constraintlayout:constraintlayout:2.1.4&#39;
implementation &#39;com.google.firebase:firebase-database:20.0.4&#39;
testImplementation &#39;junit:junit:4.13.2&#39;
androidTestImplementation &#39;androidx.test.ext:junit:1.1.5&#39;
androidTestImplementation &#39;androidx.test.espresso:espresso-core:3.5.1&#39;
}

enter image description here

buildscript {
repositories {
// Make sure that you have the following two repositories
google()  // Google&#39;s Maven repository
mavenCentral()  // Maven Central repository
}
dependencies {
classpath &#39;com.google.gms:google-services:4.3.15&#39;
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id &#39;com.android.application&#39; version &#39;7.4.0&#39; apply false
id &#39;com.android.library&#39; version &#39;7.4.0&#39; apply false
}

`

I tried all the methods that are available on internet but still realtime database is not updating. Anyone who can help me in this matter..

答案1

得分: 1

确保在FirebaseDatabase的getInstance()方法中不传递任何参数。

FirebaseDatabase fd = FirebaseDatabase.getInstance();
确保你的项目通过提供的google-services.json文件连接到Firebase控制台。

英文:

Pass no parameters in getInstance() of FirebaseDatabase

FirebaseDatabase fd= FirebaseDatabase.getInstance();
make sure your project is connected with your Firebase console through the google-services.json file provided.

huangapple
  • 本文由 发表于 2023年5月17日 18:16:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定