com.alibaba.fastjson.JSONException: write javaBean error
环境
对类对象进行序列化,转换成json string做后续处理,在JSON.toJSONString的时候,报下面的异常导致APP奔溃了
# Bean对象的定义如下
public class AppBean implements Serializable{
private Long id;
private String name;
private String pkgname;
private Integer version;
private String filepath;
private Long filesize;
private Drawable icon;
. . .
分析
这个问题十有八九就是序列化的问题,对于所定义的这个Bean,有可能不能进行正常序列化的是Drawable,那么猜测的解决办法有:
- 一个是将Drawable转为二进制存储(转Bitmap,再转bytes);
- 一个是忽略这个字段,因为请求远程的Bean,我是不需要传这个字段的
日志
2019-04-11 16:44:11.567 27693-27693/com.jeff.demo E/CrashReport: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jeff.demo/com.jeff.demo.activity.DemoActivity}: com.alibaba.fastjson.JSONException: write javaBean error, fastjson version 1.1.70
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread H . h a n d l e M e s s a g e ( A c t i v i t y T h r e a d . j a v a : 1593 ) a t a n d r o i d . o s . H a n d l e r . d i s p a t c h M e s s a g e ( H a n d l e r . j a v a : 105 ) a t a n d r o i d . o s . L o o p e r . l o o p ( L o o p e r . j a v a : 164 ) a t a n d r o i d . a p p . A c t i v i t y T h r e a d . m a i n ( A c t i v i t y T h r e a d . j a v a : 6541 ) a t j a v a . l a n g . r e f l e c t . M e t h o d . i n v o k e ( N a t i v e M e t h o d ) a t c o m . a n d r o i d . i n t e r n a l . o s . Z y g o t e H.handleMessage(ActivityThread.java:1593) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote H.handleMessage(ActivityThread.java:1593)atandroid.os.Handler.dispatchMessage(Handler.java:105)atandroid.os.Looper.loop(Looper.java:164)atandroid.app.ActivityThread.main(ActivityThread.java:6541)atjava.lang.reflect.Method.invoke(NativeMethod)atcom.android.internal.os.ZygoteMethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: com.alibaba.fastjson.JSONException: write javaBean error, fastjson version 1.1.70
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:693)
at com.alibaba.fastjson.serializer.JSONSerializer.write(JSONSerializer.java:262)
at com.alibaba.fastjson.JSON.toJSONString(JSON.java:760)
at com.alibaba.fastjson.JSON.toJSONString(JSON.java:404)
at com.jeff.demo.service.ServApp.checkForUpdate(ServApp.java:67)
at com.jeff.demo.presenter.PInstalledList.getCanUpdatedAppList(PInstalledList.java:23)
at com.jeff.demo.activity.DemoActivity.onCreate(DemoActivity.java:67)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
解决办法
我选择第二种方式,在JSON序列化的时候忽略这个字段,查资料得知,可以这样做,定义Drawable的时候增加注解@JSONField(serialize=false),显式的告诉fastjson我不需要序列化这个变量,重新编译后,App正常运行了。
更多推荐
所有评论(0)