mr-nati
mr-nati

Reputation: 225

Cannot get Android app to connect to Firebase

So I've tried this 3 times, and have had no luck successfully making an Android application that will communicate with Firebase. As simple as the tutorial makes it, I can't help but think either I or it (most likely the former) is missing something.

The sample app I'm making simply holds a TextView that should be updated with any changes to my Firebase at https://dummy-firebase.firebaseio.com/ (since I can't make a public Firebase, if you'd like you can paste the URL of your own firebase to test the code). Here's a snapshot of what's on the Firebase:

Firebase snapshot

Here's the onCreate of my only Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Firebase f = new Firebase("https://dummy-firebase.firebaseio.com");
    f.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot arg0) {
            TextView textViewSample = (TextView) findViewById(R.id.sampleTextView);
            textViewSample.setText(arg0.getValue(String.class));
        }

        @Override
        public void onCancelled() {
            // TODO Auto-generated method stub
        }
    });
}

And here's the only XML file, activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/sampleTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

And here's a picture of the Manifest so you can clearly see the INTERNET permission:

enter image description here

Now as I change the data on the Firebase from my browser, I believe the data in the TextView should update. This isn't happening, and I have unsuccessfully tried the to put data using EditTexts and Buttons in the previous Firebase Android project attempts.

Thanks in advance for taking the time to look over this. Because of how simple Firebase seems to set up (and was for the Javascript version) I'm truly stumped as to what I'm doing wrong!

Thanks!!

Upvotes: 3

Views: 10117

Answers (2)

Amrah Anam
Amrah Anam

Reputation: 58

Try this code. It is using a pojo class.

  buttonSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Creating firebase object
            Firebase ref = new Firebase(Config.FIREBASE_URL);

            //Getting values to store
            String name = editTextName.getText().toString().trim();
            String address = editTextAddress.getText().toString().trim();

            //Creating Person object
            Person person = new Person();

            //Adding values
            person.setName(name);
            person.setAddress(address);

            //Storing values to firebase
            ref.child("Person").setValue(person);


            //Value event listener for realtime data update
            ref.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot snapshot) {
                    for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                        //Getting the data from snapshot
                        Person person = postSnapshot.getValue(Person.class);

                        //Adding it to a string
                        String string = "Name: "+person.getName()+"\nAddress: "+person.getAddress()+"\n\n";

                        //Displaying it on textview
                        textViewPersons.setText(string);
                    }
                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {
                    System.out.println("The read failed: " + firebaseError.getMessage());
                }
            });

        }
    });

Source: Firebase Android Tutorial

Upvotes: 0

Greg Soltis
Greg Soltis

Reputation: 1434

Can you try with the latest SDK (v1.0.2) and see if it fixes the problem? We just added a workaround for some of Android's SSL limitations.

https://www.firebase.com/docs/downloads.html

Upvotes: 4

Related Questions