从FirebaseDatabase检索数据时遇到了一些麻烦。
我认为我已经以正确的方式完成了所有事情,但仍然会弹出此错误并导致应用崩溃。
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;
}
}
和检索的代码是
@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);
所以......我解决了这个问题。我没有使用正确的节点引用,我的错误
(按照Shubhank的要求修复)
问题是我想获得user1的个人资料节点
public static DatabaseReference mFirebaseRef = FirebaseDatabase.getInstance().getReferenceFromUrl(constants.FIREBASE_URL+"app/authGplus/users/");
所以我做的是:
mFirebaseRef.child(UserToWhichProfileIsNeeded).child("profile").addChildEventListener(listener);
但我打电话给addChildEventListener
它已经引用子节点所以我不需要在引用期间放置.child("profile")
,它会变成这样,
mFirebaseRef.child(UserToWhichProfileIsNeeded).addChildEventListener(listener);
然后它工作正常。
答案 0 :(得分:3)
首先将dataSnapshot转换为Map<String, Object>
,然后将其转换为ExtraProfilClass
Map<String, Object> map = (HashMap<String, Object>) dataSnapshot.getValue();