GSON数据解析异常报错java.lang.IllegalArgumentException: Unable to create converter for

创维v5机型gson解析数据造成应用闪退

异常log要点:java.lang.NoSuchMethodError: com.google.gson.Gson.newJsonReader

异常log要点:java.lang.IllegalArgumentException: Unable to create converter for

项目使用框架:retrofit2+rxjava2+gson

测试环境:Android4.4 HISIV510

gson依赖版本:2.6.2 / 2.8.2

github提问链接:https://github.com/google/gson/issues/1552

stackoverflow提问链接:https://stackoverflow.com/questions/57052546/java-lang-illegalargumentexception-unable-to-create-converter-for

 

通过抓到的log日志,初步判断是gson数据解析异常,所以本人首先尝试自定义TypeAdapter,继承read和write方法去手动解析json,这个方法成功的解决了报错的数据接口解析失败

Gson builder = new GsonBuilder()
     .registerTypeAdapter(ColumnInfo.class, new ColumnInfoAdapter())
     .registerTypeAdapter(PhoneWorksInfo.class, new PhoneWorksInfoAdapter(context))
     .create();

但是,另一个数据接口也报错了,这样子的话可能所有json数据接口都需要写一个TypeAdapter,工作量太大了,只能放弃这种解决思路。

 

解决方案:

通过分析app在V5机型的安装日志,发现如下信息:

07-18 11:05:22.388 D/dalvikvm( 4392): DexOpt: 'Lcom/google/gson/ExclusionStrategy;' has an earlier definition; blocking out
​
07-18 11:05:22.390 D/dalvikvm( 4392): DexOpt: 'Lcom/google/gson/FieldAttributes;' has an earlier definition; blocking out
​
07-18 11:05:22.390 D/dalvikvm( 4392): DexOpt: 'Lcom/google/gson/FieldNamingStrategy;' has an earlier definition; blocking out
​
07-18 11:05:22.390 D/dalvikvm( 4392): DexOpt: 'Lcom/google/gson/Gson;' has an earlier definition; blocking out
​
07-18 11:05:22.390 D/dalvikvm( 4392): DexOpt: 'Lcom/google/gson/GsonBuilder;' has an earlier definition; blocking out
​
07-18 11:05:22.390 D/dalvikvm( 4392): DexOpt: 'Lcom/google/gson/InstanceCreator;' has an earlier definition; blocking out
​
07-18 11:05:22.390 D/dalvikvm( 4392): DexOpt: 'Lcom/google/gson/JsonDeserializationContext;' has an earlier definition; blocking out

应用使用的是设备本身更早版本的gson库,而不是app中配置版本的gson库,所以决定在项目中手动集成gson。

 

首先在github上把gson源码拉下来,并且导入module到项目中,然后把com.squareup.retrofit2:converter-gson中的三个类也添加到项目中,并且都改掉包名,成功解决问题!

你可能感兴趣的:(Android)