Ken Lee
Ken Lee

Reputation: 51

Android OkHttp HTTP FAILED: java.io.IOException: unexpected end of stream

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?

Upvotes: 4

Views: 14159

Answers (3)

rwst
rwst

Reputation: 2685

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.

Upvotes: 0

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

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

Upvotes: 0

余健洲
余健洲

Reputation: 21

You can try this fix:

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

Upvotes: 2

Related Questions