Android confuses compilation when parsing JSON data with Gson-2.2.2.jar for reflection, classcastexception error is reported

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.