java.lang.IllegalStateException: closed 异常分析及解决

问题描述

使用okhttp请求数据,进行解析,遇到java.lang.IllegalStateException: closed 异常,错误堆栈如下

2022-02-21 15:28:17.537 14686-14972/com.xxE/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
    Process: com.xx, PID: 14686
    java.lang.IllegalStateException: closed
        at okio.RealBufferedSource.select(RealBufferedSource.java:93)
        at okhttp3.internal.Util.bomAwareCharset(Util.java:467)
        at okhttp3.ResponseBody.string(ResponseBody.java:181)
        at com.xx.HealthHeroView$2.onResponse(HealthHeroView.java:211)
        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:764)


问题分析

response.body().string()只能请求一次,请求过后,就会关闭,再次调用response.body().string()就会报close异常,

问题解决

String data = response.body().string();
使用变量接收数据,后续使用变量data 就可以解决这个问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值