Gson toJson FromJson 异常

ITprivate 2015-12-28

当一个Bean里面 还有 WeakReference 或者 SoftReference,使用Gson 2.5的 toJson 或者FromJson会导致:

 StackOverflowError 错误

参见 这个http://blog.csdn.net/s278777851/article/details/9850677

原因:

GSON版本 2.2.2, 在android 4.2之后会报错StackOverflowError,最后发现是Gson的bug,https://code.google.com/p/google-gson/issues/detail?id=440

Comment #12:

I believe it was in API 17 that the internals of WeakReference and SoftReference changed to be self-referent, which would trigger this. Are you using Gson to serialize a WeakReference or SoftReference? If you are, you should write your own TypeAdapter for those types.

解决方法一: 未测试

我用 GSON 1.7.1版本替换掉2.2.2版本就解决了。 1.7.1下载地址 https://code.google.com/p/google-gson/downloads/detail?name=google-gson-1.7.1-release.zip&can=2&q=

解决方法二:

// 用于界面更新
public transient WeakReference<View> m_oViewL = null;

相关推荐

nalanrumeng / 0评论 2020-04-09