Retrofit+Rxjava报Stackoverflow的一种情况的解决方法

最近在学习使用retrofit+rxjava,在给一个现有项目写测试app时,使用了retrofit+rxjava进行网络请求。app中有个下拉刷新功能,下拉刷新时会请求同一接口数十次,在测试时发现当连续下拉刷新十余次后,程序发生崩溃。在抓取到日志后发现是Stackoverflow:

E/AndroidRuntime: FATAL EXCEPTION: RxIoScheduler-9
                                               java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread.
                                                   at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:59)
                                                   at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)
                                                   at java.util.concurrent.FutureTask.run(FutureTask.java:234)
                                                   at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:153)
                                                   at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267)
                                                   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
                                                   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
                                                   at java.lang.Thread.run(Thread.java:856)
                                                Caused by: java.lang.StackOverflowError
                                                   at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:147)
                                                   at java.lang.StringBuilder.append(StringBuilder.java:216)
                                                   at libcore.io.ErrnoException.getMessage(ErrnoException.java:50)
                                                   at libcore.io.ErrnoException.rethrowAsSocketException(ErrnoException.java:63)
                                                   at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:552)
                                                   at libcore.io.IoBridge.recvfrom(IoBridge.java:516)
                                                   at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
                                                   at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
                                                   at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
                                                   at okio.Okio$2.read(Okio.java:140)
                                                   at okio.AsyncTimeout$2.read(AsyncTimeout.java:238)
                                                   at okio.RealBufferedSource.exhausted(RealBufferedSource.java:55)
                                                   at okhttp3.internal.io.RealConnection.isHealthy(RealConnection.java:379)
                                                   at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
                                                   at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:93)
                                                   at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:296)
                                                   at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
                                                   at okhttp3.RealCall.getResponse(RealCall.java:243)
                                                   at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:201)
                                                   at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:203)
                                                   at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:190)
                                                   at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:203)
                                                   at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:190)
                                                   at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:203)
                                                   at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:190)
                                                   at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:203)
                                                   at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:190)
                                                   at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:203)
                                                   at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:190)
                                                   at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:203)
                                                   at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:190)
                                                   at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:203)
                                                   at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:190)
                                                   at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:203)
                                                   at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:190)
                                                   at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:203)
                                                   at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:190)
                                                   at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:203)
                                                   at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:190)
                                                   at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:203)
                                                   at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:190)
                                                   at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:203)
                                                   at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:190)
                                                at okhttp3.logging.HttpLoggingIn  

经过大量查询后无果。对retrofit添加日志后发现,对一次请求打印出了多次日志,如下:

--> POST http://xx.xx.xx.xx/Api/xx/homelist http/1.1 (48-byte body)
--> POST http://xx.xx.xx.xx/Api/xx/homelist http/1.1 (48-byte body)
<-- 200 OK http://xx.xx.xx.xx/Api/xx/homelist (271ms, unknown-length body)
<-- 200 OK http://xx.xx.xx.xx/Api/xx/homelist (271ms, unknown-length body)  

且随着下拉刷新的次数增加,日志产生的数量也在增加。下载github上使用retrofit的代码打印日志后发现没有此类情况,然后发现他们代码中api接口对象是单例的。转回头查看自己的代码,发现果然问题出在这:

XXService = ServiceGenerator.createService(XXService.class);
XXService.getHongBaoList(token)  

有两个方法中,在请求前重复创建了XXService!!!

将代码中XXService修改为单例后,解决问题。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值