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)