Gson 缺少对象 ArrayList 的类型参数

Gson missing type parameter for ArrayList of objects

我有这个 JSON 对象,其中包含 "Contact" 对象及其子对象的列表。我正在尝试使用 gson 在我的 JSON 对象中获取 ArrayListModel 个对象,但它 return 缺少类型参数异常。 我得到这样的类型:

Type listType = new TypeToken<ArrayList<tModel>>() { }.getType();

并尝试获取这样的列表:

GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();

ArrayList<Model> = gson.fromJson(jsonString, listType);

在我的混淆器中,我保留了这样的对象包:

-keep class .somerepo.contactModel.** { *; }

我见过类似的问题,但 none 解决了我的问题。

这是堆栈跟踪:

FATAL EXCEPTION: AsyncTask #2 Process: ..., PID: 10360 java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask.done(AsyncTask.java:300) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) at java.util.concurrent.FutureTask.setException(FutureTask.java:222) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:841) Caused by: java.lang.RuntimeException: Missing type parameter. at com.google.gson.reflect.TypeToken.getSuperclassTypeParameter(SourceFile:84) at com.google.gson.reflect.TypeToken.(SourceFile:62) at semereop.contact.Contact.(SourceFile:184) at somerepo.contact.Contact.geModelFromJson(SourceFile:184)

geModelFromJson 方法 return ArrayList<Model> 来自 gson.

您必须向混淆文件添加更多信息:

# Gson uses generic type information stored in a class file when working with fields. 
# Proguard removes such information by default, so configure it to keep all of it.
-keepattributes Signature
-keepattributes EnclosingMethod
-keepattributes InnerClasses
-keepattributes Annotation

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
-keep class sun.misc.Unsafe { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }

有关更多详细信息,请查看 GSON 存储库中提供的 Proguard 文件的 this example