Gustavomcls
Gustavomcls

Reputation: 1655

Return a value stored in Firebase Android

I read several answers about this problem, but I still have null in the answer of Firebase Android App. One of them was:

Firebase retrieve/read returns null values - Android

My problem:

I can write into Firebase but I cannot read the values.

I try:

1 change the values of Firebase rules from:

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
} 

to:

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

but did not worked, I can write but cannot read values.

My code:

 private void postComment() {
        //final String uid = getUid();
        FirebaseDatabase.getInstance().getReference().child("users")/*.child("KU1y_SLZGLZpJB2j_Pc")*/
                .addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        // Get user information
                        UserClassGu user = dataSnapshot.getValue(UserClassGu.class);
                        String authorName = user.name;
                        Log.d(TAG,"name read into postCopmment = "+authorName);


                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
    }

This is my Class used in getValue(UserClassGu.class)

/** * Created by Cliente on 14/10/2016. */

public class UserClassGu {

    public String userUid;
    public   String email;
    public String name;
    public String telephone;

    public UserClassGu() {

    }

    public String getUserUid() {
        return userUid;
    }

    public UserClassGu(String userUid, String email, String name, String telephone) {
        userUid = userUid;
        this.email = email;
        this.name = name;
        this.telephone = telephone;
    }

    public void setUserUid(String userUid) {
        userUid = userUid;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }


}

My database:

{
  "messages" : {
    "-KU87z9hSXew-Li37xgF" : {
      "destinatario" : "[email protected]",
      "name" : "app app",
      "photoUrl" : "https://lh5.googleusercontent.com/-jjv4PSa0NzA/AAAAAAAAAAI/AAAAAAAAAAA/APaXHhQm8WxqRupmfbe87U11m9pfn9C9ag/s96-c/photo.jpg",
      "text" : "hello"
    },
    "-KU88ciYGXz39PZGZW98" : {
      "destinatario" : "rere",
      "name" : "app app",
      "photoUrl" : "https://lh5.googleusercontent.com/-jjv4PSa0NzA/AAAAAAAAAAI/AAAAAAAAAAA/APaXHhQm8WxqRupmfbe87U11m9pfn9C9ag/s96-c/photo.jpg",
      "text" : "adafa"
    }
  },
  "messages_private" : {
    "rere" : {
      "553xVAMEVcQZPKNVqfal8S9tOM03" : {
        "-KU88ciZUNQkCskad6Xc" : {
          "destinatario" : "rere",
          "name" : "app app",
          "photoUrl" : "https://lh5.googleusercontent.com/-jjv4PSa0NzA/AAAAAAAAAAI/AAAAAAAAAAA/APaXHhQm8WxqRupmfbe87U11m9pfn9C9ag/s96-c/photo.jpg",
          "text" : "adafa"
        }
      }
    }
  },
  "users" : {
    "-KU3ZlBN-gmHZOhg74-M" : {
      "email" : "[email protected]",
      "name" : "app app",
      "telephone" : ""
    },
    "-KU3_ZjTHJHprd36xT1u" : {
      "email" : "[email protected]",
      "name" : "app app",
      "telephone" : ""
    }
  }
}

Upvotes: 1

Views: 1370

Answers (1)

Gustavomcls
Gustavomcls

Reputation: 1655

I solved changing the path of the data into Firebase:

Into the Class UserGu:

  1. I forgot to write the getter getUserUid()

    public String getUserUid() { return userUid; }

2.The path of the database Firebase for the data I wanted to read was not ok: I changed to: mFirebaseDatabaseReference.child("users").child(mUserUid).setValue(userClassGu);

UserClassGu userClassGu = UserClassGu(mUserUid,mUserEmail,mUsername,"");   



            mFirebaseDatabaseReference.child("users").child(mUserUid).setValue(userClassGu);


            //Write data of users
            mFirebaseDatabaseReference.child("users").child(mUserUid).setValue(userClassGu);


            //Reading the data


mFirebaseDatabaseReference.child("users").child(mUserUid).addValueEventListener(
                new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        // Get user value
                        UserClassGu userClassgu = dataSnapshot.getValue(UserClassGu.class);
                        Log.d(TAG,"reading data  :userClassgu.email = "+userClassgu.email);
                        Log.d(TAG,"reading data  :userClassgu.name: = "+userClassgu.name);
                        Log.d(TAG,"reading data  :userClassgu.telephone: = "+userClassgu.telephone);
                        Log.d(TAG,"reading data  :userClassgu.userUid: = "+userClassgu.userUid);
                        // ...
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        Log.w(TAG, "getUser:onCancelled", databaseError.toException());
                        // ...
                    }
                });

In the class userClassGu I put the method:

@Exclude
public Map<String, Object> toMap() {
    HashMap<String, Object> result = new HashMap<>();
    result.put("uid", userUid);
    result.put("email", email);
    result.put("name", name);
    result.put("telephone", telephone);
    return result;
}

I put int the class UserClassGuthe text: @IgnoreExtraProperties

The code of the class userClassGu became:

package com.sciencesoftware.friends;

import com.google.firebase.database.Exclude;
import com.google.firebase.database.IgnoreExtraProperties;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Cliente on 14/10/2016.
 */
@IgnoreExtraProperties
public class UserClassGu {
    public String userUid;
    public   String email;
    public String name;
    public String telephone;

    public UserClassGu() {
// Default constructor required for calls to DataSnapshot.getValue(UserClassGu.class)

    }

    public UserClassGu(String userUid, String email, String name, String telephone) {
        this.userUid = userUid;
        this.email = email;
        this.name = name;
        this.telephone = telephone;
    }


    public String getUserUid() {
        return userUid;
    }





    public void setUserUid(String userUid) {
        this.userUid = userUid;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }


    @Exclude
    public Map<String, Object> toMap() {
        HashMap<String, Object> result = new HashMap<>();
        result.put("uid", userUid);
        result.put("email", email);
        result.put("name", name);
        result.put("telephone", telephone);
        return result;
    }



}

3. This is the JSON of Firebase:

{
  "users" : {
    "Bl0AiMMUXsV2H58lqoj0rO8457j1" : {
      "email" : "[email protected]",
      "name" : "my name",
      "telephone" : "",
      "userUid" : "Bl0AiMMUXsV2H58lqoj0rO8457j1"
    }
  }
}

Studding the documentation : The class that will have the data put into the database must: 1 Have a Constructor empty: 2 have a constructor with all the arguments of the variables -(I am not absolutely right about this) The Variables of the class must be public or must have all getters public(I am not absolutely right about this). 3 have all the getters. 4 the setter are not necessary.

I will be happy if somebody correct me if i said something wrong about this 4 things above.

Upvotes: 2

Related Questions