java.net.ProtocolException: unexpected end of stream
Asked Answered
R

6

18

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.

Receivable answered 24/8, 2015 at 13:48 Comment(5)
Try using BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "utf-8")); writer.write(...); instead of os.write(...)Burchfield
Are you using a logging interceptor? If so, remove that and run. This is because the logging interceptor exhausts the input or output stream, and then this exception is fired, when the actual processing is about to take place.Isotron
I face this issue in EmulatorQuietly
@Burchfield thinks you save my day that's work for me with huge data jsonFoolscap
Thanks @Debanjan. This is the fixed code: // NOTE: Must change Level.BODY to Level.BASIC to fix java.net.ProtocolException: unexpected end of stream when uploading file by Rx2AndroidNetworking: AndroidNetworking.enableLogging(HttpLoggingInterceptor.Level.BASIC);Speakeasy
A
10

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.

Androecium answered 14/10, 2015 at 7:28 Comment(0)
S
2

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

Saladin answered 4/6, 2022 at 21:13 Comment(0)
D
1

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.

Dramaturge answered 19/6, 2023 at 14:36 Comment(0)
O
0

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.

Oblation answered 3/8, 2020 at 22:54 Comment(1)
Could you please share example of values for that header parameters ?Willable
D
0

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.

Deprived answered 4/8, 2020 at 1:2 Comment(0)
F
0

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.)

Fashionable answered 11/8, 2020 at 6:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.