gpt4 book ai didi

android - Gson 无法解析日期

转载 作者:行者123 更新时间:2023-12-04 11:45:21 25 4
gpt4 key购买 nike

在我的应用程序中的一些用户抛出致命异常:com.google.gson.JsonSyntaxException。我使用 gson 向磁盘 LRU 缓存写入和读取数据。当我从缓存中读取数据时,我如何理解这个异常。

这就是我创建 json 实例的方式:

Gson gson = new Gson();

此异常发生在 Android 5、6、7、8 中。这是来自 Crashlytics 的统计数据。

这是来自 Crashlytics 的异常堆栈跟踪:
Fatal Exception: com.google.gson.JsonSyntaxException: Nov 5, 2019 20:18:24
at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:87)
at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:75)
at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:46)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
at com.google.gson.Gson.fromJson(Gson.java:927)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at com.google.gson.Gson.fromJson(Gson.java:813)
at com.example.data.repository.MbCacheImpl.getDashboard(MbCacheImpl.java:44)
at com.example.data.repository.DataRepository.getDashboard(DataRepository.java:64)
at com.example.ui.main.main.TabMainViewModel.loadDashboard(TabMainViewModel.java:289)
at com.example.ui.main.main.TabMainViewModel.loadData(TabMainViewModel.java:260)
at com.example.ui.base.BaseDataViewModel$onAttach$1.onAvailable(BaseDataViewModel.java:124)
at android.net.ConnectivityManager$CallbackHandler.handleMessage(ConnectivityManager.java:3108)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.os.HandlerThread.run(HandlerThread.java:61)

Caused by java.text.ParseException: Failed to parse date ["Nov 5, 2019
20:18:24"]: Invalid number: Nov
at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:274)
at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:85)
at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:75)
at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:46)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
at com.google.gson.Gson.fromJson(Gson.java:927)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at com.google.gson.Gson.fromJson(Gson.java:813)
at com.example.data.repository.MbCacheImpl.getDashboard(MbCacheImpl.java:44)
at com.example.data.repository.DataRepository.getDashboard(DataRepository.java:64)
at com.example.ui.main.main.TabMainViewModel.loadDashboard(TabMainViewModel.java:289)
at com.example.ui.main.main.TabMainViewModel.loadData(TabMainViewModel.java:260)
at com.example.ui.base.BaseDataViewModel$onAttach$1.onAvailable(BaseDataViewModel.java:124)
at android.net.ConnectivityManager$CallbackHandler.handleMessage(ConnectivityManager.java:3108)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.os.HandlerThread.run(HandlerThread.java:61)

Caused by java.lang.NumberFormatException: Invalid number: Nov
at com.google.gson.internal.bind.util.ISO8601Utils.parseInt(ISO8601Utils.java:311)
at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:129)
at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:85)
at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:75)
at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:46)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
at com.google.gson.Gson.fromJson(Gson.java:927)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at com.google.gson.Gson.fromJson(Gson.java:813)
at com.example.data.repository.MbCacheImpl.getDashboard(MbCacheImpl.java:44)
at com.example.data.repository.DataRepository.getDashboard(DataRepository.java:64)
at com.example.ui.main.main.TabMainViewModel.loadDashboard(TabMainViewModel.java:289)
at com.example.ui.main.main.TabMainViewModel.loadData(TabMainViewModel.java:260)
at com.example.ui.base.BaseDataViewModel$onAttach$1.onAvailable(BaseDataViewModel.java:124)
at android.net.ConnectivityManager$CallbackHandler.handleMessage(ConnectivityManager.java:3108)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.os.HandlerThread.run(HandlerThread.java:61)

最佳答案

Caused by java.text.ParseException: Failed to parse date ["Nov 5, 2019 20:18:24"]: Invalid number: Nov尝试设置日期格式以进行解析。Gson gson = new GsonBuilder().setDateFormat("MMM dd, yyyy HH:mm:ss").create();

关于android - Gson 无法解析日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60089859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com