android下载出现 java.net.ProtocolException: unexpected end of stream

小墙哥 2017-08-07 01:48:21
android下载出现有时候网速很慢的时候就会出现 java.net.ProtocolException: unexpected end of stream
有没有大神遇到这样的问题???求解

下载部分代码:
private void downloadApkFile() {
PicoLogUtils.d("downloadApkFile: ");
shallDownload = true;
String urlDownload = mRemoteAddress;
String dirName = Environment.getExternalStorageDirectory() + "/";
File f = new File(dirName);
if (!f.exists()) {
f.mkdir();
}

String fileName = "picovr.apk";
File file = new File(f.getAbsoluteFile() + "/" + fileName);
if (file.exists()) {
file.delete();
}
mFilePath = file.getAbsolutePath();

InputStream is = null;
OutputStream os = null;
try {
URL url = new URL(urlDownload);
URLConnection con = url.openConnection();
HttpURLConnection httpUrlConnection = (HttpURLConnection) con;
httpUrlConnection.setRequestProperty("Accept-Encoding", "identity");
httpUrlConnection.setConnectTimeout(15000);
httpUrlConnection.setReadTimeout(15000);
httpUrlConnection.connect();
is = httpUrlConnection.getInputStream();
totalBytes = httpUrlConnection.getContentLength();
PicoLogUtils.d("totalBytes = " + totalBytes);
byte[] bs = new byte[1024];
int len;
os = new FileOutputStream(mFilePath);
int count = 0;
while (shallDownload && ((len = is.read(bs)) != -1)) {
os.write(bs, 0, len);
downloadBytes += len;
PicoLogUtils.d("downloadBytes = " + downloadBytes);
count++;
if (count == 512) {
count = 0;
mHandler.sendEmptyMessage(UPDATE_PROGRESS);
}
}
mHandler.sendEmptyMessage(INSTALL_APK);
} catch (Exception e) {
PicoLogUtils.e("downloadApkFile Exception =:" + e);
mHandler.sendEmptyMessage(INSTALL_FAIL);
} finally {
if (is != null) {
try {
is.close();
} catch (Exception e) {
e.printStackTrace();
}
is = null;
}
if (os != null) {
try {
os.close();
} catch (Exception e) {
e.printStackTrace();
}
os = null;
}
}
}


错误log:
06-29 09:17:34.883 20030-5740/com.wing:filedownload W/System.err: java.net.ProtocolException: unexpected end of stream
06-29 09:17:34.883 20030-5740/com.wing:filedownload W/System.err: at okhttp3.internal.http1.Http1Codec$FixedLengthSource.read(Http1Codec.java:387)
06-29 09:17:34.883 20030-5740/com.wing:filedownload W/System.err: at okio.RealBufferedSource$1.read(RealBufferedSource.java:430)
06-29 09:17:34.883 20030-5740/com.wing:filedownload W/System.err: at java.io.InputStream.read(InputStream.java:162)
06-29 09:17:34.883 20030-5740/com.wing:filedownload W/System.err: at com.picovr.tools.download.FileDownloader.onResponse(FileDownloader.java:97)
06-29 09:17:34.883 20030-5740/com.picovr.wing:filedownload W/System.err: at okhttp3.RealCall$AsyncCall.execute(RealCall.java:141)
06-29 09:17:34.883 20030-5740/com.picovr.wing:filedownload W/System.err: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
06-29 09:17:34.883 20030-5740/com.picovr.wing:filedownload W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-29 09:17:34.883 20030-5740/com.picovr.wing:filedownload W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-29 09:17:34.883 20030-5740/com.wing:filedownload W/System.err: at java.lang.Thread.run(Thread.java:818)
...全文
2394 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
romanticaccount 2018-04-24
  • 打赏
  • 举报
回复
网络问题,重新下载

80,350

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧