Reputation: 4344
I am facing a strange issue, and I am not able to debug it out. I have implemented a logic for uploading stream of data and am using Volley for the same, I have customized a logic little bit in HurlStack
, addBodyIfExists
api,so that body of type "application/octet-stream" can be handled.
My logic is to post progress to user, so that UI can be updated indicating user progress in upload, below my logic for same.
int toRead = length; // File length
byte[] data = new byte[4096];
connection.setDoOutput(true);
if(length != -1) {
connection.setFixedLengthStreamingMode(length);
} else {
connection.setChunkedStreamingMode(4096);
}
OutputStream os;
int i;
int count;
os = connection.getOutputStream();
int progress= 0;
try {
for(i = 0; (count= is.read(data)) > 0; ++i) { // is, is not null and contains a valid input stream
os.write(data, 0, count); // at this line am getting unexpected end of stream
progress+= count;
if(i % 20 == 0) {
rs.deliverProgress(progress, 0L);
progress= 0;
}
}
os.flush();
} finally {
if(is != null) {
is.close();
}
if(os != null) {
os.close();
}
}
on executing above code am getting this, although I have verified, output stream is not null, neither do input stream, it fails in first iteration of read loop itself, am seeing it has read 4096 bytes and then trying to write the same.
java.net.ProtocolException: unexpected end of stream
at com.android.okhttp.internal.http.HttpConnection$FixedLengthSink.close(HttpConnection.java:326)
at com.android.okio.RealBufferedSink.close(RealBufferedSink.java:174)
at com.android.okio.RealBufferedSink$1.close(RealBufferedSink.java:142)
any help in debugging above will he highly appreciated.
Upvotes: 18
Views: 40576
Reputation: 11
I echo Vainquit's answer. If this is happening to you on the Android emulator it might not happen on the real device. This was happening when I was using the bypass that allowed http in development to a localhost server. When I got https set up on my real cloud server this went away. It also worked for me on the real device.
Upvotes: 1
Reputation: 2321
This may help you :
That exception is thrown by FixedLengthInputStream
when the expected number of bytes (usually set in the content-length
header of the response) is larger than the actual data in the response.
Check that the content-length
header is correct. (If you're supplying your own value for the content length, make sure it is correct.)
It would help to see your code that sets up the input stream.
Upvotes: 10
Reputation: 115
This happens for me on android emulator and doesn't happen on my physical android device. I was doing GET request to flask server running on 0.0.0.0 on my laptop from the android app.
To fix it on the emulator, add the servers ip address in the emulators proxy. see How to set up Android emulator proxy settings
The exact problem i had was unexpected end of stream retrofit
Upvotes: 3
Reputation: 148
It is a little strange that your data length might be unknown. Is it a media file? Or a live stream?
Anyway, I tried to upload my live stream data. And it happened in the same error. I added this setting to the Connection and solved my problem. Transfer-Encoding : chunked
("setChunkedStreamingMode" didn't work. I still don't know why.)
Upvotes: 0
Reputation: 587
If you have checked everywhere in your code and tried every solution in stackoverflow and github but the issue still occurs, and you have only tested your code on emulator, then, you should try to run your code on your real device instead. Maybe it will work, or maybe it won't, but if you feel desperate, just have a try, seriously. I was astonished when I happened to find that my code ran with bugs on emulator everytime but successfully on my mobile phone. Besides, the code also ran sucessfully on others' android emulators. So I guess there is something wrong in my android studio configuration that I can't find out. I have no idea why this happen, just like we don't know why "Clean Project/Invalidate caches" sometimes works better than any solution.
Upvotes: 0
Reputation: 1
Already Fixed it, please add "Accept-Encoding", "identity" in header, then the server-side will get command that it will not modify the response, then send back to Clients.
Upvotes: 0