Firebase Data Uploaded but Not in Database

Firebase is i of the famous backend platforms which is used by so many developers to provide backend support to their applications and websites. It is the production of Google which provides services such every bit database, storage, user authentication, and many more than. In this article, we volition create a simple app in which nosotros volition be adding our Information to Firebase Realtime Database. Note that we are going to implement this projection using theJava language.

What is Firebase Realtime Database?

Firebase Realtime Database is a NoSQL cloud database that is used to store and sync the information. The data from the database can be synced at a fourth dimension across all the clients such as android, web as well as IOS. The information in the database is stored in the JSON format and it updates in existent-time with every continued client.

What are the Advantages of using the Firebase Realtime Database?

  • The main advantage of using the Firebase Realtime database is that the data is updated in a real-time manner and you don't have to make any requests for the information updates or changes. The database uses data synchronization every time when information changes and these changes will reverberate the continued user inside milliseconds.
  • While using Firebase Realtime Database your apps remain responsive even if the device loses its connectivity to the database. In one case the user has established the connection he will receive the changes made in the data from the database.
  • The data stored in the Firebase database tin can be easily accessible through the web portal of Firebase. You can manage your database from PC as well as mobile devices. Y'all can manage the rules of the database which gives permissions to read and write operations to the database.

What We are Going to Build in This Article?

In this article, we are going to build a elementary application in which nosotros volition be getting the data from the users with the aid of some TextFields and store that data in the Firebase Realtime Database. Annotation that we are using Firebase Realtime Database and the app is written using JAVA language.

Pace by Step Implementation

Step 1: Create a New Projection

To create a new project in Android Studio please refer to How to Create/Showtime a New Project in Android Studio. Note that select Java as the programming language.

Footstep 2: Connect your app to Firebase

Later creating a new project navigate to the Tools choice on the top bar. Inside that click on Firebase. After clicking on Firebase, you tin can get to meet the right cavalcade mentioned below in the screenshot.

Inside that column Navigate to Firebase Realtime Database. Click on that choice and you will get to meet two options on Connect app to Firebase and Add Firebase Realtime Database to your app. Click on Connect now and your app volition be continued to Firebase. Later that click on the 2d choice and at present your App is connected to Firebase.

After connecting your app to Firebase you will become to see the below screen.

Later that verify that dependency for Firebase Realtime database has been added to our Gradle file. Now navigate to the app > Gradle Scripts and inside that file check whether the below dependency is added or not. If the below dependency is non added in your build.gradle file. Add the below dependency in the dependencies department.

implementation 'com.google.firebase:firebase-database:19.6.0'

After adding this dependency sync your projection and now nosotros are ready for creating our app. If you want to know more about connecting your app to Firebase. Refer to this article to get in particular nearly Adding Firebase to Android App.

Footstep three: Working with AndroidManifest.xml file

For adding data to Firebase we should have to give permissions for accessing the internet. For adding these permissions navigate to the app > AndroidManifest.xml and within that file add the below permissions to it.

XML

< uses-permission android:name = "android.permission.INTERNET" />

< uses-permission android:name = "android.permission.ACCESS_NETWORK_STATE" />

Step 4: Working with the activity_main.xml file

Go to the activity_main.xml file and refer to the following code. Below is the lawmaking for the activity_main.xml file.

XML

<? xml version = "1.0" encoding = "utf-8" ?>

< RelativeLayout

android:layout_width = "match_parent"

android:layout_height = "match_parent"

tools:context = ".MainActivity" >

< EditText

android:id = "@+id/idEdtEmployeeName"

android:layout_width = "match_parent"

android:layout_height = "wrap_content"

android:layout_centerHorizontal = "true"

android:layout_margin = "10dp"

android:hint = "Enter Employee Name"

android:importantForAutofill = "no"

android:inputType = "textPersonName" />

< EditText

android:id = "@+id/idEdtEmployeePhoneNumber"

android:layout_width = "match_parent"

android:layout_height = "wrap_content"

android:layout_below = "@id/idEdtEmployeeName"

android:layout_margin = "10dp"

android:hint = "Enter employee phone number"

android:importantForAutofill = "no"

android:inputType = "telephone" />

< EditText

android:id = "@+id/idEdtEmployeeAddress"

android:layout_width = "match_parent"

android:layout_height = "wrap_content"

android:layout_below = "@id/idEdtEmployeePhoneNumber"

android:layout_margin = "10dp"

android:hint = "Enter employee address"

android:inputType = "textPostalAddress" />

< Button

android:id = "@+id/idBtnSendData"

android:layout_width = "match_parent"

android:layout_height = "wrap_content"

android:layout_below = "@id/idEdtEmployeeAddress"

android:layout_margin = "10dp"

android:text = "Add employee details"

android:textAllCaps = "imitation" />

</ RelativeLayout >

Step 5: Create a new Java course for storing our data

For sending multiple data to the Firebase Realtime database nosotros take to create an Object class and send that whole object course to Firebase. For creating an object class navigate to the app > java > your app'southward package name > Right-click on it and Click on New > Java Class > Give a name to your class. In my case, it is EmployeeInfo, and add below lawmaking to information technology.

Java

public class EmployeeInfo {

private String employeeName;

private String employeeContactNumber;

private String employeeAddress;

public EmployeeInfo() {

}

public String getEmployeeName() {

return employeeName;

}

public void setEmployeeName(String employeeName) {

this .employeeName = employeeName;

}

public Cord getEmployeeContactNumber() {

return employeeContactNumber;

}

public void setEmployeeContactNumber(String employeeContactNumber) {

this .employeeContactNumber = employeeContactNumber;

}

public String getEmployeeAddress() {

return employeeAddress;

}

public void setEmployeeAddress(String employeeAddress) {

this .employeeAddress = employeeAddress;

}

}

Stride half-dozen: Working with the MainActivity.java file

Go to the MainActivity.java file and refer to the following lawmaking. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the lawmaking in more detail.

Coffee

import android.bone.Packet;

import android.text.TextUtils;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

import androidx.annotation.NonNull;

import androidx.appcompat.app.AppCompatActivity;

import com.google.firebase.database.DataSnapshot;

import com.google.firebase.database.DatabaseError;

import com.google.firebase.database.DatabaseReference;

import com.google.firebase.database.FirebaseDatabase;

import com.google.firebase.database.ValueEventListener;

public class MainActivity extends AppCompatActivity {

individual EditText employeeNameEdt, employeePhoneEdt, employeeAddressEdt;

individual Button sendDatabtn;

FirebaseDatabase firebaseDatabase;

DatabaseReference databaseReference;

EmployeeInfo employeeInfo;

@Override

protected void onCreate(Bundle savedInstanceState) {

super .onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

employeeNameEdt = findViewById(R.id.idEdtEmployeeName);

employeePhoneEdt = findViewById(R.id.idEdtEmployeePhoneNumber);

employeeAddressEdt = findViewById(R.id.idEdtEmployeeAddress);

firebaseDatabase = FirebaseDatabase.getInstance();

databaseReference = firebaseDatabase.getReference( "EmployeeInfo" );

employeeInfo = new EmployeeInfo();

sendDatabtn = findViewById(R.id.idBtnSendData);

sendDatabtn.setOnClickListener( new View.OnClickListener() {

@Override

public void onClick(View five) {

Cord name = employeeNameEdt.getText().toString();

String phone = employeePhoneEdt.getText().toString();

String address = employeeAddressEdt.getText().toString();

if (TextUtils.isEmpty(name) && TextUtils.isEmpty(phone) && TextUtils.isEmpty(address)) {

Toast.makeText(MainActivity. this , "Please add some data." , Toast.LENGTH_SHORT).show();

} else {

addDatatoFirebase(proper noun, phone, address);

}

}

});

}

private void addDatatoFirebase(String name, Cord phone, Cord address) {

employeeInfo.setEmployeeName(proper name);

employeeInfo.setEmployeeContactNumber(telephone);

employeeInfo.setEmployeeAddress(address);

databaseReference.addValueEventListener( new ValueEventListener() {

@Override

public void onDataChange( @NonNull DataSnapshot snapshot) {

databaseReference.setValue(employeeInfo);

Toast.makeText(MainActivity. this , "data added" , Toast.LENGTH_SHORT).show();

}

@Override

public void onCancelled( @NonNull DatabaseError error) {

Toast.makeText(MainActivity. this , "Fail to add together data " + error, Toast.LENGTH_SHORT).bear witness();

}

});

}

}

After adding this lawmaking go to this link for Firebase. After clicking on this link you volition get to see the beneath page and on this page Click on Go to Console choice in the top correct corner.

Afterwards clicking on this screen you lot will go to see the below screen with your all project within that select your project.

Within that screen click north Realtime Database in the left window.

Afterwards clicking on this pick yous volition go to meet the screen on the right side. On this page click on the Rules option which is present in the top bar. You will get to see the below screen.

Inside this screen click on the Rules tab you will get to run across the above screen and change the rules to true equally shown in the screenshot. The rules are inverse to true because we are not providing authentication within our app and nosotros accept to write data to our database. That's why we are specifying it to true. Later on changing your rules click on the publish rules button. Click on that option and your rules will be published. At present come back to the Data tab of your database.

Output:

Beneath is the video for our app for calculation information to the Firebase Realtime Database.

Run the app and make sure to connect your device to the internet. Later on that add some data in your text fields and click on the Insert data push. The data volition be added to our Firebase Database. Below is the screenshot we will get to see afterward adding information to Firebase Database from the app.


millertherited51.blogspot.com

Source: https://www.geeksforgeeks.org/how-to-save-data-to-the-firebase-realtime-database-in-android/

0 Response to "Firebase Data Uploaded but Not in Database"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel