okhttp3.internal.http.RealResponseBody

本文解决了使用OkHttp进行网络请求时出现的数据解析错误问题。通过更改数据读取方式,从使用response.body().toString()更改为response.body().string(),成功避免了因数据类型不匹配导致的解析失败。

转载请注明链接:https://blog.youkuaiyun.com/feather_wch/article/details/81407581

解决OkHttp的报错问题:okhttp3.internal.http.RealResponseBody

如果有帮助请点个赞!万分感谢!

okhttp3.internal.http.RealResponseBody

报错提示

D/OkHttp: okhttp3.internal.http.RealResponseBody @ f11e81d

原因分析

通过OkHttp请求网络,结果请求下来的数据一直无法解析并且报错,这需要将String res = response.body().toString()更改为String res = response.body().string()

解决办法

旧代码:

@Override
public void onResponse(Call call, Response response) throws IOException {
    String str = response.body().toString();
    Log.d("OkHttp", str);
}

更改为:

@Override
public void onResponse(Call call, Response response) throws IOException {
    String str = response.body().string();
    Log.d("OkHttp", str);
}
2025-11-05 13:43:02.223 20678-20678/com.tobey.zhdj W/System.err: java.io.IOException: unexpected end of stream on https://axdwechat.com/... 2025-11-05 13:43:02.223 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.java:236) 2025-11-05 13:43:02.223 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.connection.Exchange.readResponseHeaders(Exchange.java:115) 2025-11-05 13:43:02.223 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:94) 2025-11-05 13:43:02.224 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) 2025-11-05 13:43:02.224 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:43) 2025-11-05 13:43:02.224 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) 2025-11-05 13:43:02.224 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117) 2025-11-05 13:43:02.224 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:94) 2025-11-05 13:43:02.224 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) 2025-11-05 13:43:02.224 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117) 2025-11-05 13:43:02.224 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) 2025-11-05 13:43:02.224 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) 2025-11-05 13:43:02.224 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:88) 2025-11-05 13:43:02.224 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) 2025-11-05 13:43:02.224 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117) 2025-11-05 13:43:02.224 20678-20678/com.tobey.zhdj W/System.err: at com.lzy.okgo.interceptor.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:93) 2025-11-05 13:43:02.224 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) 2025-11-05 13:43:02.224 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117) 2025-11-05 13:43:02.224 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:229) 2025-11-05 13:43:02.224 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.RealCall$AsyncCall.execute(RealCall.java:172) 2025-11-05 13:43:02.224 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 2025-11-05 13:43:02.224 20678-20678/com.tobey.zhdj W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 2025-11-05 13:43:02.224 20678-20678/com.tobey.zhdj W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 2025-11-05 13:43:02.224 20678-20678/com.tobey.zhdj W/System.err: at java.lang.Thread.run(Thread.java:923) 2025-11-05 13:43:02.225 20678-20678/com.tobey.zhdj W/System.err: Caused by: java.io.EOFException: \n not found: limit=0 content=… 2025-11-05 13:43:02.225 20678-20678/com.tobey.zhdj W/System.err: at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:240) 2025-11-05 13:43:02.225 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http1.Http1ExchangeCodec.readHeaderLine(Http1ExchangeCodec.java:242) 2025-11-05 13:43:02.225 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.java:213) 2025-11-05 13:43:02.225 20678-20678/com.tobey.zhdj W/System.err: ... 23 more 2025-11-05 13:43:02.225 20678-20678/com.tobey.zhdj W/System.err: java.io.IOException: unexpected end of stream on https://axdwechat.com/... 2025-11-05 13:43:02.225 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.java:236) 2025-11-05 13:43:02.225 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.connection.Exchange.readResponseHeaders(Exchange.java:115) 2025-11-05 13:43:02.225 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:94) 2025-11-05 13:43:02.225 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) 2025-11-05 13:43:02.225 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:43) 2025-11-05 13:43:02.225 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) 2025-11-05 13:43:02.225 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117) 2025-11-05 13:43:02.225 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:94) 2025-11-05 13:43:02.225 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) 2025-11-05 13:43:02.225 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117) 2025-11-05 13:43:02.225 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) 2025-11-05 13:43:02.225 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) 2025-11-05 13:43:02.225 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:88) 2025-11-05 13:43:02.226 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) 2025-11-05 13:43:02.226 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117) 2025-11-05 13:43:02.226 20678-20678/com.tobey.zhdj W/System.err: at com.lzy.okgo.interceptor.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:93) 2025-11-05 13:43:02.226 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) 2025-11-05 13:43:02.226 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117) 2025-11-05 13:43:02.226 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:229) 2025-11-05 13:43:02.226 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.RealCall$AsyncCall.execute(RealCall.java:172) 2025-11-05 13:43:02.226 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 2025-11-05 13:43:02.226 20678-20678/com.tobey.zhdj W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 2025-11-05 13:43:02.226 20678-20678/com.tobey.zhdj W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 2025-11-05 13:43:02.226 20678-20678/com.tobey.zhdj W/System.err: at java.lang.Thread.run(Thread.java:923) 2025-11-05 13:43:02.226 20678-20678/com.tobey.zhdj W/System.err: Caused by: java.io.EOFException: \n not found: limit=0 content=… 2025-11-05 13:43:02.226 20678-20678/com.tobey.zhdj W/System.err: at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:240) 2025-11-05 13:43:02.226 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http1.Http1ExchangeCodec.readHeaderLine(Http1ExchangeCodec.java:242) 2025-11-05 13:43:02.226 20678-20678/com.tobey.zhdj W/System.err: at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.java:213) 2025-11-05 13:43:02.226 20678-20678/com.tobey.zhdj W/System.err: ... 23 more
最新发布
11-06
评论 7
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猎羽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值