I have been following a guide on how to write data to a firebase real-time database. Though I followed every instruction my app doesn't seem to work. When I click on the send button database doesn't update. I haven't received any errors so I decided to add a phew listeners but they don't work too (none of the toasts is shown). Firebase tool says that all of the dependencies are set up correctly. Firebase project is launched in test mode so the problem can't be connected with the rules. What am i doing wrong? This how my activity code looks like:
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCanceledListener;
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.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class MainActivity extends AppCompatActivity {
private EditText email,name,password;
Button btn;
private DatabaseReference ref;
private String USER_KEY="User";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
initListener();
}
public void init(){
email=findViewById(R.id.editTextTextPersonName);
name=findViewById(R.id.editTextTextPersonName2);
password=findViewById(R.id.editTextTextPersonName3);
ref = FirebaseDatabase.getInstance().getReference().child(USER_KEY);
btn =findViewById(R.id.button);
}
public void initListener(){
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String id =ref.getKey();
String ema=email.getText().toString();
String nam=name.getText().toString();
String p=password.getText().toString();
User user = new User(id,ema,nam,p);
DatabaseReference usersRef = ref.child("users");
usersRef.push().setValue(user).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this,"Failed",Toast.LENGTH_SHORT).show();
System.out.println("Failed");
}
}).addOnCanceledListener(new OnCanceledListener() {
@Override
public void onCanceled() {
Toast.makeText(MainActivity.this,"Canceled",Toast.LENGTH_SHORT).show();
System.out.println("Canceled");
}
}).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(MainActivity.this,"Succeed",Toast.LENGTH_SHORT).show();
System.out.println("Succeed");
}
}).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(MainActivity.this,"Complete",Toast.LENGTH_SHORT).show();
System.out.println("Complete");
}
});
Toast.makeText(MainActivity.this,"end",Toast.LENGTH_SHORT).show();
}
});
}
}
User class:
public class User {
public String email,password,nickName;
public User(String email, String password, String nickName) {
this.email = email;
this.password = password;
this.nickName = nickName;
}
public User() {
}
}
Gradle built file:
plugins {
id 'com.android.application'
id 'com.google.gms.google-services'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 18
targetSdkVersion 30
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 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.google.firebase:firebase-database:20.0.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
Also, this is my first time here so don't be rude pls.
Just needed to reimport google-services.json file
u can insert your project sha1 key in firebase project setting than download the google json file and save in your app folder in android studio.
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.