I have this JSON object that contains list of "Contact" objects and it's child objects. I'm trying to use gson to get the ArrayList
of Model
objects in my JSON object but it returns missing type parameter exception.
I'm getting the type like this:
Type listType = new TypeToken<ArrayList<tModel>>() { }.getType();
And trying to get list like this:
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
ArrayList<Model> = gson.fromJson(jsonString, listType);
and in my proguard I kept the object package like this:
-keep class .somerepo.contactModel.** { *; }
I've seen similar questions but none of them solved my problem.
Here is the stacktrace:
FATAL EXCEPTION: AsyncTask #2 Process: ..., PID: 10360 java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.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$1.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$1.(SourceFile:184) at somerepo.contact.Contact.geModelFromJson(SourceFile:184)
geModelFromJson
method return ArrayList<Model>
from gson.
You have to add more information to your proguard file:
# 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.** { *; }
For more details, check this example of Proguard file, provided in GSON repository.