16

getting issue when writing large video file using httpurlconnection.

java.io.IOException: unexpected end of stream on Connection{192.1.4.55, proxy=DIRECT@ hostAddress=192.1.4.55 cipherSuite=none protocol=http/1.1} (recycle count=0)
W/System.err:     at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:210)W/System.err:     at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:904)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:788)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:443)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:388)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:501)

response code is here

final InputStream is = connection.getInputStream();
            final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            final byte[] buffer = new byte[maxBufferSize];
            int bytesRead;
            while ((bytesRead = is.read(buffer, 0, 1024)) != -1) {
                bytes.write(buffer, 0, bytesRead);
            }
            log.log(INFO,
                    format("{0} took {4} ms", url,
                            (currentTimeMillis() - start)));
            String response = new String(bytes.toByteArray());
8
  • long processes should be moved to separate thread Jan 29, 2016 at 9:32
  • The server unexpectedly closed the connection instead of sending a response. Are you sure the request was valid HTTP?
    – user207421
    Jan 29, 2016 at 10:33
  • yes, its working perfectly for file size<5MB. but throwing above error when file size is large.
    – JosephM
    Jan 29, 2016 at 11:25
  • 2
    So the server is imposing a size limit.
    – user207421
    Jan 29, 2016 at 17:42
  • 1
    I don't see what the time taken has to do with it. The server is still imposing a size limit.
    – user207421
    Jan 30, 2016 at 5:00

4 Answers 4

3

Check it if you are using OkHttpClient, here is a parameter retryOnConnectionFailure(false). By default it is false, you just make it true then error will be removed. Hopefully because I have same problem and solve just change it.

OkHttpClient client = new OkHttpClient.Builder()
    .retryOnConnectionFailure(true)
    .build();
-3

Please use BufferedInputStream and BufferedOutputStream to buffer the data for larger files

0
-3

Hope this Code will Help you .

BufferedReader br = null;
if (conn.getResponseCode() == 200) {
    Log.e("HTTP success code", "" + conn.getResponseCode());
    InputStream inputStream = conn.getInputStream();
    if (inputStream != null)
        br = new BufferedReader(new InputStreamReader(inputStream));
} else {
    Log.e("HTTP error code", "" + conn.getResponseCode());
    InputStream inputStream = conn.getErrorStream();
    if (inputStream != null)
        br = new BufferedReader(new InputStreamReader(inputStream));
} 
StringBuilder builder = new StringBuilder();
String output;
while ((output = br.readLine()) != null) {
    builder.append(output);
}
Log.e(reqTag + " Response :", builder.toString());
conn.disconnect();
0
-6

Sometimes this exception can be ignore,you may need to retry.
e.g. use charles proxy you mobile phone wifi,download file,charles Terminate this connection,you will receive same exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.