【问题标题】:DatabaseException: Expected a Map while deserializing, but got a class java.lang.LongDatabaseException:反序列化时需要一个 Map,但有一个类 java.lang.Long
【发布时间】:2016-12-21 23:09:24
【问题描述】:

这个问题/错误的根本原因是什么?如何解决?我的数据模型结构不正确吗?还是我做错了查询?

// 添加新评论

String comment = medit.getText().toString();
Comment newComment = new Comment(user,comment,postKey);
db.child("Post-Comments").child(postKey).push().setValue(newComment);

// 检索帖子中的所有评论 //使用 FirebaseRecyclerView 和片段

 DatabaseReference mRef = db.child("Post-Comments").child(postKey).getRef();

 adapter = new FirebaseRecyclerAdapter<Comment, myViewHolder> (Comment.class,
R.layout.comment_list_layout,myViewHolder.class, mRef)
  {
        @Override
        protected void populateViewHolder(myViewHolder viewHolder, 
            Comment model, int position) {


            viewHolder.bindToComments(model);
        }
    };

    rlm2 = new LinearLayoutManager(getActivity());
    rv2.setLayoutManager(rlm2);
    rv2.setAdapter(adapter);

// 注释类

public class Comment {

public String comment;
public String reactUserId;
public Map<String,String> created_on;
public String postKey;



public Comment() {
}

public Comment(String reactUserId, String comment, String postKey) {

    this.reactUserId = reactUserId;
    this.postKey = postKey;
    this.comment = comment;
    this.created_on = ServerValue.TIMESTAMP;

}


public Map<String,Object> toMap(){
    Map<String,Object> values = new HashMap<>();

    values.put("userId",reactUserId);
    values.put("postKey",postKey);
    values.put("comment",comment);
    values.put("created_on",created_on);

    return values;

}
}// more getter and setters

// 堆栈跟踪

10745-10745/com.dhiraj.firebaseapp E/AndroidRuntime: FATAL EXCEPTION: main
com.google.firebase.database.DatabaseException: 
Expected a Map while    deserializing, but got a class java.lang.Long

//来自 Firebase 数据库的 JSON

{
"Post-Comments" : {
 "-KPE1qy6FgcwOnb1YXRw" : 
    "-KPE1zMFviLJvEKxexB7" : {
    "AUTHOR" : "Dheeraj",
    "comment" : "the first time I am not sure if you have any questions please ",
    "created_on" : 1471278741281,
    "postKey" : "-KPE1qy6FgcwOnb1YXRw",
    "reactUserId" : "Dheeraj"
  },
  "-KPE221Xk5t9aMJfYoKD" : {
    "AUTHOR" : "Dheeraj",
    "comment" : "the first time I am not sure if you have any questions please ",
    "created_on" : 1471278756332,
    "postKey" : "-KPE1qy6FgcwOnb1YXRw",
    "reactUserId" : "Dheeraj"
  }
}
  },
 "Posts" : {
  "-KPE1qy6FgcwOnb1YXRw" : {
  "created on" : 1471278706910,
  "desc" : "the only one who has been sent to you and your family and friends are invited to view the full image to open ",
  "title" : "the following link ",
  "userId" : "Dhiraj"
},
"-KPE1vlYc5vF7AyL2OHL" : {
  "created on" : 1471278726738,
  "desc" : "hi I was thinking that it will take place at all and all the best way is to have ",
  "title" : "good afternoon ",
  "userId" : "Dhiraj"
 }
},

}

【问题讨论】:

  • 您能否编辑您的问题以显示Comment 类的外观?
  • 引发此错误的确切行是哪一行?你能发布你的错误堆栈跟踪吗?
  • 除了前两个问题,还发了一个来自Post-Comments/&lt;postKey&gt;的JSON代表sn-p(实际JSON文本,请不要截图)。
  • 在json中“created_on”是一个很长的,但是在Comments.java中,它是一个Map..尝试将Map更改为HashMap,并在构造函数中初始化HashMap并使用hashMap.put(" created_on", ServerValue.TIMESTAMP);
  • @Linxy 谢谢,它成功了。我不知道这是我的时间戳。

标签: android firebase firebase-realtime-database


【解决方案1】:
mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference("Post-Comments")

Query queryRef = mFirebaseDatabaseReference.orderByKey();
            queryRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                        if (dataSnapshot != null && dataSnapshot.getValue() != null) {
                        Log.e("Count ", "" + dataSnapshot.getChildrenCount());
                        Map<String, Object> objectMap = (HashMap<String, Object>)
                                dataSnapshot.getValue();
                        ArrayList<ItemsReceived> itemsReceivedList = new ArrayList<>();

                        for (Object obj : objectMap.values()) {
                            if (obj instanceof Map) {
                                Map<String, Object> mapObj = (Map<String, Object>) obj;
                                ItemsReceived itemsReceived = new ItemsReceived();
                                itemsReceived.setAccount((String) mapObj.get("AUTHOR"));
                                itemsReceived.setAdded((long) mapObj.get("comment"));

                                itemsReceivedList.add(itemsReceived);

                                Log.e(TAG, "Data is" + itemsReceived.getAdded());


                            }
                        }

                        itemsRecievedListAdapter = new ItemsRecievedListAdapter(MainActivity.this, itemsReceivedList);
                        mRecyclerView.setAdapter(itemsRecievedListAdapter);
    //
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });`

在必要的地方替换我的变量我只是想提出一个观点并没有完全解决......我希望它有所帮助

【讨论】:

  • 随时谦虚的Goodlife
猜你喜欢
  • 2020-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-17
  • 2020-04-04
  • 1970-01-01
  • 1970-01-01
  • 2021-08-24
相关资源
最近更新 更多