com.firebase.client.FirebaseException: 解析节点失败 class class CLASS_NAME android

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

我在使用 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.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-to-JSON转换。

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

但是您可以做的是将 POJO 转换为 Map,其中:

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

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