当前位置:  开发笔记 > 前端 > 正文

com.firebase.client.FirebaseException:无法使用类CLASS_NAME android解析节点

如何解决《com.firebase.client.FirebaseException:无法使用类CLASS_NAMEandroid解析节点》经验,为你挑选了1个好方法。

我在使用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 timeStampLastUsed;
    @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) {
        HashMap timestampJoined = 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转换.

你传递一个HashMapupdateChildren(),这符合该API的合同.但是我很确定在某些值中你有一个Java对象(可能是a HashTagModel)没有直接映射到JSON类型.

但你可以做的是将POJO转换为Map:

Map hashtaghMap = new ObjectMapper().convertValue(hashtaghModel, Map.class);

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



1> Frank van Pu..:

setValue()方法不同,updateChildren()不对您传入的值执行Java-to-JSON转换.

你传递一个HashMapupdateChildren(),这符合该API的合同.但是我很确定在某些值中你有一个Java对象(可能是a HashTagModel)没有直接映射到JSON类型.

但你可以做的是将POJO转换为Map:

Map hashtaghMap = new ObjectMapper().convertValue(hashtaghModel, Map.class);

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

推荐阅读
mobiledu2402851203
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有