首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android OkHttp HTTP失败: java.io.IOException:流的意外结束

Android OkHttp HTTP失败: java.io.IOException:流的意外结束
EN

Stack Overflow用户
提问于 2022-04-26 04:02:31
回答 2查看 2.9K关注 0票数 2

SpringBoot服务器

收听本地主机:30000

Android客户端(Android模拟器Nexus5XAPI 30)

1.使用okHttp4.10.0-RC1、Retrofit 2.9.0

2.按以下方式发送邮件请求

请求

代码语言:javascript
运行
复制
--> POST http://10.0.2.2:30000/user/login
 Content-Type: application/json; charset=UTF-8
 Content-Length: 45
{"account":"13535853646","password":"123456"}
--> END POST (45-byte body)

3.请求内容如下

请求内容

4.但反应是空洞的。!!!在Springboot日志中没有任何反应。

响应

5.由于响应为null,OKHTTP抛出一个EOFException。

http EOFException

我在stackover中尝试了关于这个异常的所有方法,它们都没有起作用。

例如添加标题(“连接”、“关闭”)(“传输-编码”:“标识”)

代码语言:javascript
运行
复制
I/okhttp.OkHttpClient: <-- HTTP FAILED: java.io.IOException: unexpected end of stream on http://10.0.2.2:30000/...
java.io.IOException: unexpected end of stream on http://10.0.2.2:30000/...
at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.kt:204)
at okhttp3.internal.connection.Exchange.readResponseHeaders(Exchange.kt:110)
at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.kt:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at com.lijiahao.sharechargingpile2.network.interceptor.TokenHeaderInterceptor.intercept(TokenHeaderInterceptor.kt:26)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at com.android.tools.profiler.agent.okhttp.OkHttp3Interceptor.intercept(OkHttp3Interceptor.java:57)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:34)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)
2at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.kt:219)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)
at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:517)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)
Caused by: java.io.EOFException: \n not found: limit=0 content=…
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:332)
at okhttp3.internal.http1.HeadersReader.readLine(HeadersReader.kt:29)
at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.kt:180)

6.我关闭了springboot服务器,问题是一样的。

7.我可以在仿真器中使用亚行外壳,获得10.0.2.2的成功

亚行壳平

代码语言:javascript
运行
复制
adb shell
generic_x86_64:/ $ ping 10.0.2.2
PING 10.0.2.2 (10.0.2.2) 56(84) bytes of data.
64 bytes from 10.0.2.2: icmp_seq=1 ttl=255 time=4.25 ms
64 bytes from 10.0.2.2: icmp_seq=2 ttl=255 time=0.745 ms
64 bytes from 10.0.2.2: icmp_seq=3 ttl=255 time=0.630 ms
64 bytes from 10.0.2.2: icmp_seq=4 ttl=255 time=0.922 ms
64 bytes from 10.0.2.2: icmp_seq=5 ttl=255 time=0.756 ms
64 bytes from 10.0.2.2: icmp_seq=6 ttl=255 time=0.826 ms
64 bytes from 10.0.2.2: icmp_seq=7 ttl=255 time=0.618 ms

8.当我在真正的机器上运行我的应用程序时。将10.0.2.2更改为我的PC IP。它可以成功地得到响应。除了主机之外,请求内容是相同的。

在真正的机器上运行成功

邮递员

发送相同的帖子请求,并获得响应成功

问题

在Android和SpringBoot服务器中似乎没有问题。

唯一的问题是模拟器和PC本地主机之间的连接。

我不知道为什么我的安卓演播室模拟器不能访问本地主机springboot服务器?

EN

回答 2

Stack Overflow用户

发布于 2022-08-24 05:34:06

您可以尝试此修复:

代码语言:javascript
运行
复制
OkHttpClient client = new OkHttpClient.Builder()
    .retryOnConnectionFailure(true)
    .build();
票数 0
EN

Stack Overflow用户

发布于 2022-09-12 14:32:34

如果在实际设备上发生此错误,则可以尝试以下操作:

代码语言:javascript
运行
复制
val httpInterceptor = HttpLoggingInterceptor()
httpInterceptor.level = HttpLoggingInterceptor.Level.BODY
okBuilder.addInterceptor(httpInterceptor)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72008226

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档