gpt4 book ai didi

android - com.firebase.client.FirebaseException : Failed to parse node with class class CLASS_NAME android

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:27:24 26 4
gpt4 key购买 nike

我在使用 updateChildren 方法更新 Firebase 中的现有值时出现以下异常。

com.firebase.client.FirebaseException: Failed to parse node with class class com.shajeelafzal.quicktasks_app.models.HashTagModel
at com.firebase.client.snapshot.NodeUtilities.NodeFromJSON(NodeUtilities.java:84)
at com.firebase.client.snapshot.NodeUtilities.NodeFromJSON(NodeUtilities.java:12)
at com.firebase.client.utilities.Validation.parseAndValidateUpdate(Validation.java:127)
at com.firebase.client.Firebase.updateChildren(Firebase.java:438)
at com.shajeelafzal.quicktasks_app.fragments.AddEditTaskFragment$4.onDataChange(AddEditTaskFragment.java:408)

我的模型是这样的:

public class HashTagModel implements Parcelable {

private HashMap<String, Object> timeStampLastUsed;
@Expose
private String name;
@Expose
private String createByUserEmail;
private List<String> tasksKeys;

public HashTagModel() {
}

public HashTagModel(HashMap<String, Object> timeStampLastUsed, String name,
String createByUserEmail, ArrayList<String> tasksKeys) {
this.timeStampLastUsed = timeStampLastUsed;
this.name = name;
this.createByUserEmail = createByUserEmail;
this.tasksKeys = tasksKeys;
}
}

我想更新的 JSON 对象在 Firebase 上看起来像这样:

"hashTags" : {
"USER_EMAIL" : {
"USA" : {
"createByUserEmail" : "USER_EMAIL",
"name" : "#USA",
"tasksKeys" : [ "-K6mS36uhKthKf1-1pF1" ],
"timeStampLastUsed" : {
"timestamp" : 1451514461234
}
}
}
}

我的 onDateChange 方法如下所示:

public void onDataChange(DataSnapshot dataSnapshot) {
/** if the hash tag does not exists already then create new one. */
if (dataSnapshot.getValue() == null) {
HashMap<String, Object> timestampJoined = new HashMap<>();
timestampJoined.put(Constants.FIREBASE_PROPERTY_TIMESTAMP, ServerValue.TIMESTAMP);

ArrayList<String> keysList = new ArrayList<String>();
keysList.add(key);

HashTagModel hashTag = new HashTagModel(timestampJoined, "#" + mHashTags.get(finalI),
Utils.decodeEmail(mEncodedEmail), keysList);
finalHashTagLocation.setValue(hashTag);
} else {

HashTagModel hashtaghModel = dataSnapshot.getValue(HashTagModel.class);

hashtaghModel.getTasksKeys().add(key);

/* HashMap for data to update */
HashMap<String, Object> updateUserTaskData = new HashMap<>();


Utils.updateMapForAllWithValue(null, mHashTags.get(finalI), mEncodedEmail,
updateUserTaskData, "", hashtaghModel, Constants.FIREBASE_LOCATION_USER_HASH_TAGS);


/** update the Hash Tag */
finalHashTagLocation.updateChildren(updateUserTaskData, new Firebase.CompletionListener() {
@Override
public void onComplete(FirebaseError firebaseError, Firebase firebase) {
Log.i("", "");
}
});
}

getActivity().finish();
}

最佳答案

不同于setValue()方法,updateChildren()不会对您传入的值执行 Java 到 JSON 的转换。

您传递的是 HashMap<String, Object>进入updateChildren() ,这符合该 API 的约定。但我很确定在某些值中,您有一个未直接映射到 JSON 类型的 Java 对象(可能是 HashTagModel)。

但是你可以做的是将 POJO 转换成一个 Map :

Map<String, Object> hashtaghMap = new ObjectMapper().convertValue(hashtaghModel, Map.class);

然后您可以将值映射放入要传递给 updateChildren() 的值中.

关于android - com.firebase.client.FirebaseException : Failed to parse node with class class CLASS_NAME android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34538330/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com