Samir
Samir

Reputation: 45

Read Write to Firebase Real time database

I have connected to Firebase shown under Save and Retrieve Data in Tools >> Firebase. My project is in android studios using Java.

This is the error I get from logcat and the code below:

12-16 11:42:42.699 2485-2550/? E/PlayCommon: [120] com.google.android.play.a.g.a(466): Failed to connect to server: java.net.UnknownHostException: Unable to resolve host "play.googleapis.com": No address associated with hostname 12-16 11:43:00.005 1534-1547/? E/memtrack: Couldn't load memtrack module

This package com.example.sammay.firebaseapplication;

 import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.text.TextUtils;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Spinner;
    import android.widget.Toast;

    import com.google.firebase.database.DatabaseReference;
    import com.google.firebase.database.FirebaseDatabase;

    public class MainActivity extends AppCompatActivity {

        EditText editTextName;
        Button buttonAdd;
        Spinner spinnerGenres;

        DatabaseReference databaseArtist;

        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

    //        databaseArtist = FirebaseDatabase.getInstance().getReference("artists");

            editTextName = (EditText)findViewById(R.id.editTextName);
            buttonAdd = (Button)findViewById(R.id.buttonAddArtist);
            spinnerGenres = (Spinner)findViewById(R.id.spinnerGenres);

            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference myRef = database.getReference("root");

            myRef.setValue("Hello, World!");

            buttonAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    addArtist();
                }
            });

        }

        private void addArtist(){
            String name = editTextName.getText().toString().trim();
            String genre = spinnerGenres.getSelectedItem().toString();

            if(!TextUtils.isEmpty(name)){

                String id = databaseArtist.push().getKey();

                Artist artist = new Artist(id, name, genre);

                databaseArtist.child(id).setValue(artist);

                Toast.makeText(this, "Artist added", Toast.LENGTH_LONG).show();

            }else{
                Toast.makeText(this, "You should enter a name", Toast.LENGTH_LONG).show();
            }
        }
    }

I have also created the necessary class Artist with the said details. Everything seems to look correct but I when I launch the app and enter details and click the button nothing happens to the real-time database.

Upvotes: 0

Views: 290

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

Most common reason for getting the UnknownHostException is the missing Internet-Permission in your AndroidManifest.xml file. To solve this, please add the following line of code right after your package name:

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

Upvotes: 2

Related Questions