java - "java.lang.IllegalArgumentException "尝试检索文档数据

标签 java android firebase timestamp google-cloud-firestore

这是日志猫:

java.lang.IllegalArgumentException: method group.onservice.onservice.GetData.setTimestamp argument 1 has type java.sql.Timestamp, got java.util.Date at java.lang.reflect.Method.invoke(Native Method) at com.google.android.gms.internal.zzevb$zza.zza(Unknown Source:134) at com.google.android.gms.internal.zzevb.zza(Unknown Source:437) at com.google.android.gms.internal.zzevb.zza(Unknown Source:2) at com.google.firebase.firestore.DocumentSnapshot.toObject(Unknown Source:9) at group.onservice.onservice.ProfileActivity$2.onEvent(ProfileActivity.java:155) at group.onservice.onservice.ProfileActivity$2.onEvent(ProfileActivity.java:146) at com.google.firebase.firestore.zzi.onEvent(Unknown Source:16) at com.google.android.gms.internal.zzevc.zza(Unknown Source:6) at com.google.android.gms.internal.zzevd.run(Unknown Source:6) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

这是发生错误的代码:

firebaseFirestore.collection("Posts").addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {

                for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {
                    if (doc.getType() == DocumentChange.Type.ADDED) {
                        AdPost adPost = doc.getDocument().toObject(AdPost.class);
                        ad_list.add(adPost);

                        adRecyclerAdapter.notifyDataSetChanged();
                    }
                }
            }
        });

我实际上不知道如何编程,所以我在一些论坛和视频中搜索了如何做到这一点(我对编程有点陌生),事实是:我看到每个人都使用相同的代码,并且它对所有人都有效,除了我。因此,没有人报告任何类型的错误。有人可以帮助我理解我的代码有什么问题吗?

PS:指定错误的确切行是这样的:

AdPost adPost = doc.getDocument().toObject(AdPost.class);

AdPost.class:

package group.onservice.onservice;

import java.sql.Timestamp;

public class AdPost {

    String des, image_url, titu, user_id, valo;
    Timestamp timestamp;

    public AdPost() {

    }

    public AdPost(String des, String image_url, String titu, String user_id, String valo, Timestamp timestamp) {
        this.des = des;
        this.image_url = image_url;
        this.titu = titu;
        this.user_id = user_id;
        this.valo = valo;
        this.timestamp = timestamp;
    }

    public String getDes() {
        return des;
    }

    public void setDes(String des) {
        this.des = des;
    }

    public String getImage_url() {
        return image_url;
    }

    public void setImage_url(String image_url) {
        this.image_url = image_url;
    }

    public String getTitu() {
        return titu;
    }

    public void setTitu(String titu) {
        this.titu = titu;
    }

    public String getUser_id() {
        return user_id;
    }

    public void setUser_id(String user_id) {
        this.user_id = user_id;
    }

    public String getValo() {
        return valo;
    }

    public void setValo(String valo) {
        this.valo = valo;
    }

    public Timestamp getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Timestamp timestamp) {
        this.timestamp = timestamp;
    }

}

最佳答案

代码中的问题是您使用的是 Timestamp 对象而不是 Date 对象作为时间戳。 Timestamp 类不是 supported data type在Firestore。为了解决这个问题,我建议你看看我的答案 post ,我在其中解释了如何使用 Date 类将时间戳存储在 Firebase 实时数据库中。您将看到有两种方法,一种使用模型类,另一种使用 map 和 FieldValue.serverTimestamp()

关于java - "java.lang.IllegalArgumentException "尝试检索文档数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51574112/

相关文章:

Java:用于缓存计算结果的数据结构?

java - 根据点赞数和创建时间对实体进行评分

android - 如何防止在我的 flutter 应用程序的特定部分截屏?

xamarin - Xamarin 中的 Firebase 通知

java - 显示弹出窗口时 setVolumeControlStream 不起作用

android - 如何以编程方式安装特定语言的 tts 语言数据?

java - 由 : java. lang.IllegalStateException 引起:应为 BEGIN_ARRAY 但在第 1 行第 2 列为 BEGIN_OBJECT

firebase - 使用 Geofire + Firebase 过滤结果

javascript - 在 JavaScript 中从环境变量中解析字符串

java - 访问 JavaFX 中的 Controller ,然后执行另一个类中的函数