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

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)

    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记