Android OKHTTP HTTP失败:java.io.ioexception:流的意外结束

发布于 2025-01-24 03:20:30 字数 4422 浏览 2 评论 0原文

SpringBoot服务器

收听Local主机:30000

Android Client(Android Studio Emulator Nexus 5X API 30)

1。使用Okhttp 4.10.0.0-RC1,reTRofit 2.9.0

2。发送如下

--> POST http://10.0.2.2:30000/user/login
 Content-Type: application/json; charset=UTF-8
 Content-Length: 45
{"account":"135******46","password":"123456"}
--> END POST (45-byte body)

​nofollow noreferrer“>请求内容

4。但是响应为空! Springboot日志中没有任何反应。

响应

5。由于响应为null,所以okhttp抛出了eofexception。

http eofexception
我尝试了关于stackover中此例外的所有方法,它们没有起作用。
例如添加标头(“连接”,“关闭”)(“转移 - 编码”:“身份”)

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。我可以使用ADB Shell在模拟器

adb shell ping

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。它可以成功获得响应。除主机外,请求内容是相同的。

在真实机器中运行成功

Postman

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

问题,

似乎那里似乎存在在Android客户端和Springboot服务器中毫无疑问。
唯一的问题是模拟器和PC Localhost之间的连接。
我不知道为什么我的Android Studio仿真器无法访问Localhost Springboot服务器?

SpringBoot Server

listening to localhost:30000

Android Client (Android Studio Emulator Nexus 5X API 30)

1. using okhttp 4.10.0-RC1、Retrofit 2.9.0

2. send the post request as below

request

--> POST http://10.0.2.2:30000/user/login
 Content-Type: application/json; charset=UTF-8
 Content-Length: 45
{"account":"135******46","password":"123456"}
--> END POST (45-byte body)

3. the request content is below

request content

4. but the response is empty。 !!! And there is no any reaction in Springboot log.

response

5. because the response is null, OKHTTP throw a EOFException.

http EOFException
I try the all the method about this Exception in stackover, they didn't work.
such as add Header ("Connection", "close") ("Transfer-Encoding": "identity")

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. I turn off the springboot server, the problem is same

7. I can ping 10.0.2.2 success using adb shell in emulator

adb shell ping

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. When I run my app in real machine. Changing 10.0.2.2 to my PC IP. It can get the response successfully. The request content is same except Host.

run success in real machine

Postman

send same post request, and get response success

Question

It seems that there is no question in Android Client and SpringBoot Server.
The only problem is the connection between emulator and PC localhost.
I don't know why my android studio emulator can't access localhost springboot server?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧人 2025-01-31 03:20:30

您可以尝试此修复程序:

OkHttpClient client = new OkHttpClient.Builder()
    .retryOnConnectionFailure(true)
    .build();

You can try this fix:

OkHttpClient client = new OkHttpClient.Builder()
    .retryOnConnectionFailure(true)
    .build();
枕花眠 2025-01-31 03:20:30

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

val httpInterceptor = HttpLoggingInterceptor()
httpInterceptor.level = HttpLoggingInterceptor.Level.BODY
okBuilder.addInterceptor(httpInterceptor)

if this error occurs on a real device, then you can try this:

val httpInterceptor = HttpLoggingInterceptor()
httpInterceptor.level = HttpLoggingInterceptor.Level.BODY
okBuilder.addInterceptor(httpInterceptor)
终止放荡 2025-01-31 03:20:30

就我而言,我开始在HTTP请求期间更改的范围内的Cor​​outine中进行改造,从而取消了Coroutine。

因此,请检查您是否在可能更改的范围内调用改造。

In my case I started retrofit in a coroutine inside a scope that changed during the HTTP request, cancelling the coroutine with it.

So, check if you call retrofit inside a scope that possibly changed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文