Java.Io.Ioexception: Unexpected End of Stream on Connection in Android

Retrofit POST java.io.IOException: unexpected end of stream on Connection caused by java.io.EOFException: \n not found:

Short story

The problem was with the server I was hitting. It was not sending any response (literally nothing. No headers, no body, nothing).

Long story

So after going through all the related answers on stackoverflow, other good websites and trying out so many solution which I have mentioned in the question itself, it did not solve my issue.

After carefully reading the stack trace, I came across the following line.

okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:203)

The client (my code) is trying to read the Response header and that's when the error java.io.EOFException: \n not found: limit=0 content= is thrown.

This gave me a hint that the problem could be with the server and not with the client. So I thought I should try with a different client and see if I can see the raw response.

The first tool that came to my mind was Curl (Postman used to give the generic Could not get any response and this did not happen consistently). I hit the server using curl with verbose option and boom! I got the following response:

curl -v --location --request POST 'http://XX.XXX.XXX.9:8085/psc/document_upload' --form 'document=@/home/user376/Downloads/test-1.pdf' --form 'document_id=22004494_ae7f_4998_a1d8_73249bda9905'
Note: Unnecessary use of -X or --request, POST is already inferred.
* Trying XX.XXX.XXX.9...
* Connected to XX.XXX.XXX.9 (XX.XXX.XXX.9) port 8085 (#0)
> POST /psc/document_upload HTTP/1.1
> Host: XX.XXX.XXX.9:8085
> User-Agent: curl/7.49.0
> Accept: */*
> Content-Length: 4684053
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------a8446c7eedb10689
>
< HTTP/1.1 100 Continue
* Empty reply from server
* Connection #0 to host XX.XXX.XXX.9 left intact
curl: (52) Empty reply from server

And that confirmed the problem was with the server and not with the client(Retrofit / http).

Moral of the story: Sometimes you have to read the stacktrace word by word even if it doesn't seem worth looking in to :)

unexpected end of stream retrofit

It seems that the problem is with the Android Studio emulator. I have tried connecting my smartphone to android studio and installing the APK on another smartphone and the problem does not reproduce.



Related Topics



Leave a reply



Submit