Code for registration
//Code used for inserting the data of students I am not adding import parts because this question is not submitted at that time.
public class StudentregisterActivity extends AppCompatActivity {
EditText stname;
EditText stphone;
EditText stemail;
EditText stpassword;
Button stbtn;
DatabaseReference studentref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.studentregister);
stname=findViewById(R.id.editTextTextPersonName);
stemail=findViewById(R.id.editTextTextEmailAddress);
stphone=findViewById(R.id.editTextPhone);
stpassword=findViewById(R.id.editTextTextPassword);
stbtn=findViewById(R.id.button7);
studentref=FirebaseDatabase.getInstance().getReference().child("Students");
stbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
InsertStudentData();
}
});
}
private void InsertStudentData(){
String name=stname.getText().toString();
String email=stemail.getText().toString();
String phone=stphone.getText().toString();
String password=stpassword.getText().toString();
Students students=new Students(name,email,phone,password);
studentref.push().setValue(students);
Toast.makeText(StudentregisterActivity.this,"Register Successfully",Toast.LENGTH_SHORT).show();
}
}
Students
package com.example.pariksha;
public class Students {
String name;
String email;
String phone;
String password;
public Students(String name, String email, String phone, String password) {
this.name = name;
this.email = email;
this.phone = phone;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
setting-gradle
//Gradle setting of the project
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
repositories {
google()
mavenCentral()
jcenter()
maven { url "https://maven.xyz.com" }
}
}
rootProject.name = "Pariksha"
include ':app'
gradle-properties
//Gradle properties of the project
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
android.useAndroidX=true
android.enableJetifier=true
gradle-build app
//Gradle properties of app
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'com.google.gms.google-services'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.example.pariksha"
minSdk 19
targetSdk 31
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
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleCompatible
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-auth:19.2.0'
implementation 'com.google.firebase:firebase-database:19.2.1'
implementation 'com.google.firebase:firebase-database:20.0.3'
implementation platform('com.google.firebase:firebase-bom:29.0.3')
implementation 'com.google.firebase:firebase-analytics'
implementation 'com.google.firebase:firebase-analytics:20.0.2'
testImplementation 'junit:junit:4. '
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
}
build gradle project
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.4"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0"
classpath 'com.google.gms:google-services:4.3.10'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
CodePudding user response:
First of all please have a look at rules defined for Firebase Realtime Database. If you're not using Firebase Auth, You can Update rules like this.
Goto Realtime Database then change tab to Rules and update rules.
{
"rules": {
".read": true,
".write": true
}
}
Then you can write data.
private void InsertStudentData(){
String name=stname.getText().toString();
String email=stemail.getText().toString();
String phone=stphone.getText().toString();
String password=stpassword.getText().toString();
Students students=new Students(name,email,phone,password);
studentref.setValue(students).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(StudentregisterActivity.this,"Register Successfully",Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(StudentregisterActivity.this,"Register failed",Toast.LENGTH_SHORT).show();
}
});
}
CodePudding user response:
Make sure following things
- Internet permission in Manifest.
- Set Firebase Realtime database Rules to Public/Authenticate user for private rules.
- Internet Availability on testing device to push data into database.
To set Internet Permission
<uses-permission android:name="android.permission.INTERNET" />
To set rules to public
- Goto Firebase console
- Realtime database rules
- Paste this code in rules
**
{
“rules”: {
“.read”: true,
“.write”: true
}
}
**
At the end try uploading data using the code below
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference tasksRef = rootRef.child("Students").push();
Students students=new Students(name,email,phone,password);
tasksRef.setValue(student);
