使用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的约定。但是我很确定在某些值中您有一个Java对象(可能是HashTagModel)没有直接映射到JSON类型。

但是您可以做的是使用以下命令将POJO转换为Map:

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

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

10-05 17:49