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

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 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题