英文:
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("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'
}
buildscript {
repositories {
// Make sure that you have the following two repositories
google() // Google's Maven repository
mavenCentral() // Maven Central repository
}
dependencies {
classpath 'com.google.gms:google-services:4.3.15'
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.4.0' apply false
id 'com.android.library' version '7.4.0' 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论