com.alibaba.fastjson.JSONException: write javaBean error

Environmental Science

Serialize the class object and convert it to json string for subsequent processing. During JSON.toJSONString, the following exception is reported, which causes the APP to crash

# The Bean object is defined as follows
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;
    . . .

Analysis

Nine times out of ten, the problem is serialization. For the defined Bean, it may not be able to perform normal serialization. The guesswork solution is as follows:

  • One is to convert Drawable to binary storage (to Bitmap, then to bytes);
  • One is to ignore this field, because I don't need to pass it to the remote Bean

Journal

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.ActivityThreadH.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.ZygoteH.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.ZygoteH.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)

Solution

I choose the second method, which ignores this field during JSON serialization. After checking the data, I can do this. When defining Drawable, add the annotation @ JSONField(serialize=false). Explicitly tell fastjson that I don't need to serialize this variable. After recompilation, the App runs normally.

Tags: Java Android JSON

Posted on Fri, 29 Nov 2019 07:24:57 -0800 by hmgroen