小墙哥 2017-06-29 01:36 采纳率: 0%
浏览 5661

android下载出现unexpected end of stream

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;
        }
    }
}
有没有大神遇到这样的问题???求解
  • 写回答

1条回答

  • 小墙哥 2017-06-29 01:34
    关注

    错误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)

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大