2

I'm having a bit trouble retrieving data from FirebaseDatabase.

I think I have done every thing in a correct manner but still This error pops up and crashes the app.

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.rana.sahaj.myyu.profile.ExtraProfilClass

ExtraProfilClass

@IgnoreExtraProperties
public class ExtraProfilClass {
private String branch;
private String campus;
private String course;
private String gplusURL;
private String hashname;
private String picurl;
private String picurl50DP;
private String userEmail;
private String userNAME;
private String yearFrom;
private String yearTo;
public ExtraProfilClass(){
    //Empty constructor for firebase
}


public ExtraProfilClass(String branch, String campus, String course, String gplusURL, String hashname, String picurl, String picurl50DP, String userEmail, String userNAME, String yearFrom, String yearTo) {
    this.branch = branch;
    this.campus = campus;
    this.course = course;
    this.gplusURL = gplusURL;
    this.hashname = hashname;
    this.picurl = picurl;
    this.picurl50DP = picurl50DP;
    this.userEmail = userEmail;
    this.userNAME = userNAME;
    this.yearFrom = yearFrom;
    this.yearTo = yearTo;
}

public String getBranch() {
    return branch;
}

public void setBranch(String branch) {
    this.branch = branch;
}

public String getCampus() {
    return campus;
}

public void setCampus(String campus) {
    this.campus = campus;
}

public String getCourse() {
    return course;
}

public void setCourse(String course) {
    this.course = course;
}

public String getGplusURL() {
    return gplusURL;
}

public void setGplusURL(String gplusURL) {
    this.gplusURL = gplusURL;
}

public String getHashname() {
    return hashname;
}

public void setHashname(String hashname) {
    this.hashname = hashname;
}

public String getPicurl() {
    return picurl;
}

public void setPicurl(String picurl) {
    this.picurl = picurl;
}

public String getPicurl50DP() {
    return picurl50DP;
}

public void setPicurl50DP(String picurl50DP) {
    this.picurl50DP = picurl50DP;
}

public String getUserEmail() {
    return userEmail;
}

public void setUserEmail(String userEmail) {
    this.userEmail = userEmail;
}

public String getUserNAME() {
    return userNAME;
}

public void setUserNAME(String userNAME) {
    this.userNAME = userNAME;
}

public String getYearFrom() {
    return yearFrom;
}

public void setYearFrom(String yearFrom) {
    this.yearFrom = yearFrom;
}

public String getYearTo() {
    return yearTo;
}

public void setYearTo(String yearTo) {
    this.yearTo = yearTo;
}
}

and the code for retrieving is

 @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
      // Map<String, String> msg = (HashMap<String, String>)dataSnapshot.getValue();
 -->       ExtraProfilClass extraProfilClass=dataSnapshot.getValue(ExtraProfilClass.class);

    //    String userEmail_here=extraProfilClass.getUserEmail();
   //     userEmailKey = userEmail_here.substring(0, userEmail_here.length() - 10);

So.. I fixed the problem. I was not using correct node reference, my bad

(Fix as requested by Shubhank)

Problem was that i wanted to get the profile node of user1

public static DatabaseReference mFirebaseRef = FirebaseDatabase.getInstance().getReferenceFromUrl(constants.FIREBASE_URL+"app/authGplus/users/");

so what i did was: mFirebaseRef.child(UserToWhichProfileIsNeeded).child("profile").addChildEventListener(listener);

but as i am calling addChildEventListener

it already refers to child node so i don't need to put .child("profile") during refernce and it would become like this,

mFirebaseRef.child(UserToWhichProfileIsNeeded).addChildEventListener(listener);

and then it worked fine.

Sahaj Rana
  • 1,993
  • 4
  • 25
  • 42

1 Answers1

3

first convert dataSnapshot to Map<String, Object> then convert it to your ExtraProfilClass

Map<String, Object> map = (HashMap<String, Object>) dataSnapshot.getValue();
Mehul Kabaria
  • 6,404
  • 4
  • 25
  • 50
  • I have tried that after it was not working i came to it.. n by the way that way is not longer supported.. using maps – Sahaj Rana Jun 15 '16 at 07:34