0

I am downloading audio files by using AsyncTask. Files are downloading on high speed internet like wifi but app gives error with end of stream on low speed internet like mobile data. My download file code is:

`URLConnection  conexion = urle.openConnection();
conexion.connect();
InputStream input = new BufferedInputStream(conexion.getInputStream());
totalSize = conexion.getContentLength();
OutputStream output = new FileOutputStream(st);
byte data[] = new byte[1024];
while ((count = input.read(data)) != -1) 
{
  output.write(data, 0, count);
  downloadedSize += count;
}                               
output.flush();
output.close();
input.close();`

Following exception is coming:

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

help

From this link error is due to (usually set in the content-length header of the response) is larger than the actual data in the response. But how can solve it???

Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74

1 Answers1

0

I faced the similar issue. it is better to use Download Manager

DownloadManager automatically tries to download if there is network change and works on slow internet connections.

Anup Dasari
  • 488
  • 1
  • 4
  • 13
  • Thanks for your kind answer. But during downloading after about 70% this error is coming: "unfortunately the process android.process.media has stopped"???? – Naveed Ahmed Jan 10 '16 at 05:08
  • This is due to unstable network. So sometimes the internet is very slow that the app cannot get the response for downloading. – Anup Dasari Jan 12 '16 at 05:46