当互联网连接丢失时,读取Inputstream不停止

Kir*_*dev 3 android inputstream android-asynctask

我正在使用asynctask来下载文件.它正常工作,直到我关闭我的Android的wifi连接(没有其他互联网连接),仍然没有更改下载对话框.当我通过日志检查时,我发现输入流的函数read()是不停止的.那么如何检查这种情况呢?这是我的代码:

URL url = new URL(this.url);
        URLConnection connection = url.openConnection();
        connection.setReadTimeout(1000);
        connection.connect();
        // this will be useful so that you can show a typical 0-100% progress bar
        int fileLength = connection.getContentLength();
        fileName = "temp.zip";

        // download the file
        InputStream input = new BufferedInputStream(connection.getInputStream());
        OutputStream output = new FileOutputStream(path+fileName);

        byte buffer[] = new byte[1024000];
        long total = 0;
        int count;
        Log.v("test download:","download in background");
        while (((count = input.read(buffer)) != -1)) {
            Log.v("test download:","read:"+count);
            total += count;
            publishProgress((int) (total * 100 / fileLength - 1));
            output.write(buffer, 0, count);
        }
Run Code Online (Sandbox Code Playgroud)

Joe*_*Joe 8

由于您已经通过调用设置了超时setReadTimeout(),因此您应该SocketTimeoutException在连接断开后立即获得.

您的代码是否可能会默默捕获此异常?