java.net.ProtocolException:意外的流结束

Tec*_*ist 10 android android-volley okhttp

我面临一个奇怪的问题,我无法调试它.我已经实现用于上载的数据的流,并且正在使用排球对于相同的逻辑,我已经定制的逻辑点点HurlStack,addBodyIfExistsAPI,所以类型"application /八位字节流"的该机构可以处理.

我的逻辑是将进度发布给用户,以便可以更新UI,指示用户在上传中的进度,低于我的逻辑.

            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();
                    }

                }
Run Code Online (Sandbox Code Playgroud)

执行上面的代码得到这个,虽然我已经验证,输出流不是null,输入流也没有,它在读取循环本身的第一次迭代中失败,我看到它已读取4096字节然后尝试写入相同.

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)
Run Code Online (Sandbox Code Playgroud)

任何有关上述调试的帮助都将受到高度赞赏.

Sae*_*eid 5

可能对您有所帮助:

当预期的字节数(通常在响应的内容长度标头中设置)大于响应中的实际数据时,FixedLengthInputStream抛出该异常.检查内容长度标头是否正确.(如果您要为内容长度提供自己的值,请确保它是正确的.)

查看设置输入流的代码会有所帮助.