In my code, I use the following method:
Public synchronized throws exception{ null; New Gson (); = Gson.fromjson (JSON, classoft); return target; }
Then the way I use it in my activity is:
String Addmodel = Jsonparse.getjsonparse (). Parsejson (Jsonstring, Addmodel. Class);
Where Addmodel is a model class of his own writing
Jsonstring is a local file that exists in JSON format
After the general compilation is not a problem, but confusing the compilation will be error: shortMsg:java.lang.ClassCastException
LongMsg:java.lang.ClassCastException:com.google.gson.internal.StringMap
StackTrace:java.lang.ClassCastException:com.google.gson.internal.StringMap
At COM.ANDROID.XXXX.ACTIVITY.INFO.C (Unknown Source)
At Com.android.xxxx.activity.Info.onClick (Unknown Source)
At Android.view.View.performClick (view.java:2486)
At Android.view.view$performclick.run (view.java:9130)
At Android.os.Handler.handleCallback (handler.java:587)
At Android.os.Handler.dispatchMessage (handler.java:92)
At Android.os.Looper.loop (looper.java:130)
At Android.app.ActivityThread.main (activitythread.java:3703)
At Java.lang.reflect.Method.invokeNative (Native Method)
At Java.lang.reflect.Method.invoke (method.java:507)
At Com.android.internal.os.zygoteinit$methodandargscaller.run (zygoteinit.java:866)
At Com.android.internal.os.ZygoteInit.main (zygoteinit.java:624)
At Dalvik.system.NativeStart.main (Native Method)This is due to the use of Gson reflection parsing the JSON string, which causes the member variable name to not be corresponding to the JSON string because of the confusion of the model.Workaround:in the Proguard.cfg filefirst, do not confuse the corresponding model classFor example, I use the name Addmodel:
Public class com.android.chebaob.model.addmodel{ *;}
And to add at the end of the Proguard.cfg file:
# #---------------Begin:proguard Configuration forGson----------# Gson uses generic type information stored in aclassfile when working. proguard# removes such information bydefault, so configure it to keep all of it.-keepattributes signature# for using Gson @Expose annotation-keepattributes *annotation*# Gson Specific Classes-keepclassSun.misc.Unsafe {*; } #-keepclasscom.google.gson.stream.** {*; } # application Classes that'll be serialized/deserialized over Gson-keepclasscom.google.gson.examples.android.model.** {*; } ##---------------End:proguard Configuration forGson----------
That's going to solve the problem.