Gson无法解析日期

pw9qyyiw  于 2022-11-06  发布在  其他
关注(0)|答案(2)|浏览(187)

在我的应用程序中的一些用户抛出了Fatal Exception:我使用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)
z0qdvdin

z0qdvdin1#

Gson gson = new GsonBuilder().setDateFormat("MMM dd, yyyy HH:mm:ss").create();
请尝试设置日期格式以进行分析。
Gson gson = new GsonBuilder().setDateFormat("MMM dd, yyyy HH:mm:ss").create();

nfg76nw0

nfg76nw02#

我只是添加到gson自定义日期serialiazer和deserialiazer,我用@P.Juni回答,它也帮助我。

相关问题