无法使用推送 Id Firebase 弹回类型

Failed to bounce to type with push Id Firebase

原因:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“-Jzp5XCUx78BX5D7BvkU

我的 POJO class

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

public class ChatData {
    String messageId;
    String userName;
    String currentDate;
    String chatType;
    String messageType;
    String currentLocation;


    public ChatData() {

    }

    public ChatData(String messageId, String userName, String currentDate, String chatType, String messageType, String currentLocation) {
        this.messageId = messageId;
        this.userName = userName;
        this.currentDate = currentDate;
        this.chatType = chatType;
        this.messageType = messageType;
        this.currentLocation = currentLocation;
    }

    public String getMessageId() {
        return messageId;
    }

    public String getUserName() {
        return userName;
    }

    public String getCurrentDate() {
        return currentDate;
    }

    public String getChatType() {
        return chatType;
    }

    public String getMessageType() {
        return messageType;
    }

    public String getCurrentLocation() {
        return currentLocation;
    }


}

消息接收方

public static final String CHAT_TYPE_FRIEND = "friend";

和当前频道=6ca5c08c-9dd2-4f8f-8475-34a427c2354f

  // User node listener were all the user private messages arriving
    private void listenToUserNode() {

        try {
            Firebase mFirebaseRef = new Firebase(Constants.FIRE_BASE_URL).child(Constants.CHAT_TYPE_FRIEND + File.separator + currentChannel);
            mFirebaseRef.addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {


                }

                @Override
                public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                    String key = dataSnapshot.getKey();
                    ChatData newMessage = dataSnapshot.getValue(ChatData.class);
                    handleReceivedMessage(currentChannel,newMessage);
                }

                @Override
                public void onChildRemoved(DataSnapshot dataSnapshot) {

                }

                @Override
                public void onChildMoved(DataSnapshot dataSnapshot, String s) {

                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

从 firebase onChild 接收数据时遇到错误,因为我的 pojo 与数据匹配但它抛出错误,因为没有推送 ID 的字段

请注意,此答案基于您发布的不完整代码。如果您添加设置 Firebase 引用的代码并调用 getValue() 到您的问题,那将非常有帮助。

您似乎正在尝试将聊天消息列表读入ChatData 对象:

Firebase messagesRef = new Firebase("https://yours.firebaseio.com/messages");
messagesRef.addValueEventListener(new ValueEventListener() {
    public void onDataChange(DataSnapshot snapshot) {
        ChatData data = snapshot.getValue(ChatData.class);
    }
    public void onCancelled(FirebaseError firebaseError) {
    }
}

那不行。

您有两个选择:

循环阅读每条消息

在此代码段中,我们将使用 snapshot.getChildren() 迭代各个消息。

messagesRef.addValueEventListener(new ValueEventListener() {
    public void onDataChange(DataSnapshot snapshot) {
        for (DataSnapshot child: snapshot.getChildren()) {
            ChatData data = child.getValue(ChatData.class);
        }
    }
    public void onCancelled(FirebaseError firebaseError) {
    }
}

将所有消息读入映射

或者我们可以将所有消息读入映射:

messagesRef.addValueEventListener(new ValueEventListener() {
    public void onDataChange(DataSnapshot snapshot) {
        GenericTypeIndicator<Map<String,ChatData>> chatsType = new GenericTypeIndicator<Map<String,ChatData>>() {};
        Map<String,ChatData> chats = child.getValue(Map<String,ChatData>);
    }
    public void onCancelled(FirebaseError firebaseError) {
    }
}

如果您不关心消息的推送 ID,您可以 chats.values()。但在那种情况下,您可能需要考虑为什么首先要存储带有推送 ID 的消息,而不是使用您自己的 messageId 值。