gpt4 book ai didi

java - Gson().fromJson() 错误取决于操作系统版本

转载 作者:行者123 更新时间:2023-12-02 02:02:21 26 4
gpt4 key购买 nike

我正在使用 firebase DB 编写 Android 应用程序,并在 android api 22 上测试它,它可以工作,但是当我尝试在 api 26(xiaomi mi 6)上启动它时,它终止了。我猜问题出在 Gson().fromJson() 上。

API 26 错误:

                      --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.easyshop, PID: 10051
com.google.gson.JsonSyntaxException: Jul 17, 2018 11:35:06
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.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.example.user.easyshop.MainActivity$1.onDataChange(MainActivity.java:64)
at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source:13)
at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source:2)
at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source:71)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6665)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
Caused by: java.text.ParseException: Failed to parse date ["Jul 17, 2018 11:35:06"]: Invalid number: Jul
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.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.example.user.easyshop.MainActivity$1.onDataChange(MainActivity.java:64) 
at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source:13) 
at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source:2) 
at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source:71) 
at android.os.Handler.handleCallback(Handler.java:789) 
at android.os.Handler.dispatchMessage(Handler.java:98) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6665) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781) 
Caused by: java.lang.NumberFormatException: Invalid number: Jul
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.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.example.user.easyshop.MainActivity$1.onDataChange(MainActivity.java:64) 
at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source:13) 
at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source:2) 
at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source:71) 
at android.os.Handler.handleCallback(Handler.java:789) 
at android.os.Handler.dispatchMessage(Handler.java:98) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6665) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781) 
I/Process: Sending signal. PID: 10051 SIG: 9
Application terminated.

(com.example.user.easyshop.MainActivity$1.onDataChange(MainActivity.java:64) at) 关于我的代码的此错误:

@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

Type listType = new TypeToken<ArrayList<Item>>(){}.getType();
List<Item> yourClassList = new Gson().fromJson(dataSnapshot.getValue(String.class), listType); //this line
Config.LIST = (ArrayList<Item>) yourClassList;

}

我的元素.class:

public class Item implements Comparable<Item>{

private String name;
private boolean bought;
private boolean deleted;
private Date addDate;
private Date operationDate;


public Item(){
this.name = "Item";
bought = false;
deleted = false;
addDate = new Date();
operationDate = null;
}

public Item(String name){
this.name = name;
bought = false;
deleted = false;
addDate = new Date();
operationDate = null;

@Override
public int compareTo(@NonNull Item o) {
int actual = isBought() ? 1 : 0;
int other = o.isBought() ? 1 : 0;
if(actual == other)
return name.compareTo(o.name);
else
return actual - other;
}
...
}

正如我所说,一切都可以在其他设备上运行。它可能是什么?

最佳答案

Caused by: java.text.ParseException: Failed to parse date ["Jul 17, 2018 11:35:06"]: Invalid number: Jul 
.....
Caused by: java.lang.NumberFormatException: Invalid number: Jul

您可以使用 dateFormat 模板进行日期解析示例:

 Gson gson = new GsonBuilder()
.setDateFormat("MM dd, yyyy HH:mm:ss")
.create();

https://developer.android.com/reference/java/text/SimpleDateFormat

关于java - Gson().fromJson() 错误取决于操作系统版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51381381/

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