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.

4
  • check this link stackoverflow.com/questions/32108969/…; Commented Jun 15, 2016 at 7:23
  • 1
    but now firebase don't use Jackson for serialization and deserialization Commented Jun 15, 2016 at 7:28
  • 1
    post the answer of fix instead of updating question with the fix. Commented Jun 15, 2016 at 13:39
  • okie i wil udate it . Commented Jun 15, 2016 at 15:36

1 Answer 1

3

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

Map<String, Object> map = (HashMap<String, Object>) dataSnapshot.getValue();
Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.