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
How to Have 1 Firebase Database for 2 Apps with Different Package Names
Android UI Design: Supporting Multiple Screens
Searchview Getactionview Returning Null
Android: Google Maps Location with Low Battery Usage
Getting Header from Response (Retrofit/Okhttp Client)
Can Gradientcolor Be Used to Define a Gradient for a Fill or Stroke Entirely in Xml
Opening Infowindow Automatically When Adding Marker Google Maps V2 Android
Communicating with Serial Usb Device Over Android
Android: Broadcastreceiver Intent to Detect Camera Photo Taken
How to Set Up Multiple Listeners for One Event
Select Distinct Value in Android SQLite
Android Decoder->Decode Returned False for Bitmap Download
Play Downloaded Gif Image in Android
Relocate Google Logo in Mapview
Custom Title Bar Without Padding (Android)
Viewing Excel Files in My Android App
Lint Error "Do Not Treat Position as Fixed; Only Use Immediately..."