使用retrofit+rxjava报java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread

引言:如果还没有接触retrofit+rxjava的同学赶紧学起来吧!真的炒鸡好用辣。。。

好了,言归正传。最近才接触的retrofit和rxjava,所以在实践过程中会遇到一些之前没有遇到过的
问题,昨天因为第三方包的原因搞了一整天才解决,所以今天就来给大家分享一下。
首先,我导入的包是:

compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
compile 'io.reactivex:rxjava:1.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.1.2'
compile 'com.squareup.okhttp3:okhttp:3.4.1' compile 'com.google.code.gson:gson:2.6.2'

然后在声明retrofit的时候我设置了一下log拦截器

addInterceptor(new HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY));

然后运行的时候就懵逼了

E/AndroidRuntime: FATAL EXCEPTION: RxCachedThreadScheduler-1
Process: com.jack.mc.cyg.retrofittestproject, PID: 30089
    java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread.
    ..........(还有很多错误日志)
     Caused by: java.lang.NoClassDefFoundError: okhttp3.internal.Platform

在这里我们不用去关心其他的错误日志,只需要看什么异常,看造成异常的原因就可以了。昨天看到这个的时候我就很奇怪,明明导了okhttp3这个包啊!为什么会报错呢,然后我就习惯性的build项目了,后来发现还是不行,然后我就在网上疯狂的下载retrofit的源码,后来我发现大多数人在声明retrofit的时候都没有设置log拦截器,然后我就跟风,把log拦截器给干掉了,后来发现竟然可以正常运行了,然后我又懵逼了,这跟log拦截器有什么关系啊???然后我就试着去谷歌那个错误,结果在stackoverflow上找到了发生错误的原因,是说导入的包版本不匹配,这么一说,再来看之前导入的包,哪些包不匹配呢,我估计大家都猜出来了,肯定是有log拦截器的包啊!然后我就又重新导入okhttp3和logging-interceptor这两个包:

compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
compile 'io.reactivex:rxjava:1.1.0'
compile 'com.squareup.okhttp3:okhttp:3.3.1'
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'
compile 'com.google.code.gson:gson:2.6.2'

然后重新运行,bingo,完美

在此附上查阅的网站网址:

点我查阅

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值