我在使用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 HashMaptimeStampLastUsed; @Expose private String name; @Expose private String createByUserEmail; private List tasksKeys; public HashTagModel() { } public HashTagModel(HashMap timeStampLastUsed, String name, String createByUserEmail, ArrayList 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) { HashMaptimestampJoined = new HashMap<>(); timestampJoined.put(Constants.FIREBASE_PROPERTY_TIMESTAMP, ServerValue.TIMESTAMP); ArrayList keysList = new ArrayList (); 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 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(); }
Frank van Pu.. 14
与setValue()
方法不同,updateChildren()
不对您传入的值执行Java-to-JSON转换.
你传递一个HashMap
到updateChildren()
,这符合该API的合同.但是我很确定在某些值中你有一个Java对象(可能是a HashTagModel
)没有直接映射到JSON类型.
但你可以做的是将POJO转换为Map:
MaphashtaghMap = new ObjectMapper().convertValue(hashtaghModel, Map.class);
然后,您可以将值映射放入要传递的值中updateChildren()
.
与setValue()
方法不同,updateChildren()
不对您传入的值执行Java-to-JSON转换.
你传递一个HashMap
到updateChildren()
,这符合该API的合同.但是我很确定在某些值中你有一个Java对象(可能是a HashTagModel
)没有直接映射到JSON类型.
但你可以做的是将POJO转换为Map:
MaphashtaghMap = new ObjectMapper().convertValue(hashtaghModel, Map.class);
然后,您可以将值映射放入要传递的值中updateChildren()
.