0

我有使用以下doInBackground方法的 AsyncTask 。这应该是读取和写入流。问题是当连接断开时 IOException 在buffer为空之前被调用(它在之前离开while循环len != -1),我想避免这种情况。

protected Boolean doInBackground(String... StringUrls) {

    try {

   // ...

 len = in.read(buffer);

        while (len != -1) {

            bufOutstream.write(buffer, 0, len);
            len = in.read(buffer);

            if (Recorder.this.isCancelled)  {

            Recorder.this.stopSelf();
                break;
            }

        }


        bufOutstream.close();

    } catch (IOException e) {
        System.err.println("Caught IOException: " + e.getMessage());

        // 
    }


   return true;
}
4

3 回答 3

0

抛出异常时,len 保存上一次读取迭代的值,即最后一次成功。

无论如何,我建议:

    try
    {
        while ( (len = in.read()) > -1 )
        {

            bufOutstream.write(buffer, 0, len);

            if (Recorder.this.isCancelled)  
            {

                Recorder.this.stopSelf();
                break;
            }
        }
    }
    catch ( Exception e )
    {
        System.err.println("Caught IOException: " + e.getMessage());
    }
    finally
    {
        if ( in != null )
        {
            try
            {
                in.close();
            }
            catch ( Exception e )
            {
                // And I don't caaare
            }
        }

        if ( bufOutstream != null )
        {
            try
            {
                bufOutstream.close()
            }
            catch ( Exception e )
            {
                // And I don't caaare
            }
        }
    }
于 2013-11-07T13:49:54.783 回答
0

如果不想退出 while 循环,则需要将 try catch 块移到 while 循环内。不过,我建议以异常退出循环。这是我将使用的代码:

protected Boolean doInBackground(String... StringUrls) {
    Boolean success = true;
    len = 0;

    try {
        while (len != -1) {
            bufOutstream.write(buffer, 0, len);
            len = in.read(buffer);

            if (Recorder.this.isCancelled)  {

            Recorder.this.stopSelf();
                break;
            }
        }
    } catch (IOException e) {
            // do something with the exception and if you can't handle it here 
            // rethrow it!
            success = false;
    } finally {
        try {
            bufOutstream.close();
        } catch (Exception e) {
            // do something with the exception and if you can't handle it here 
            // rethrow it!
        }
    }

    return success;
}
于 2013-11-07T14:19:54.577 回答
0

在这种情况下,您可以检查您的 while 循环中的条件:
if您发现即使len != -1没有变为 false,也会抛出异常,
然后吞下 IOException,else您可以独占抛出它。

受保护的布尔doInBackground(字符串... StringUrls){

        尝试 {
            //...
            int len = in.read(buffer);

            而(len!= -1){
                尝试{
                bufOutstream.write(buffer, 0, len);
                len = in.read(buffer);

                if (Recorder.this.isCancelled) {
                Recorder.this.stopSelf();
                    休息;
                }
                }catch(IOException ioe){
                    如果(len == -1){
                        抛出新的 IOException(ioe);
                    }
                        /*IOException 在不满足 len != -1 的情况下被吞掉*/
                }
            }
            bufOutstream.close();

        } 捕捉(IOException e){
            System.err.println("捕获 IOException:" + e.getMessage());
            // ...
        }
    }
于 2013-11-07T14:20:36.910 回答