Android - 终结者抛出的未捕获异常

时间:2013-01-22 07:36:15

标签: android apache-httpclient-4.x fileoutputstream

我有这段代码接受服务器响应并将其写入文件。

该文件包含json数据。我将响应写入文件,以便按顺序扫描json并避免在List中加载大的json数据!

我觉得这种方法会引发异常,但我不确定!

public File getData(final String url) throws URISyntaxException, AuthenticationException, IOException, ClientProtocolException, HttpResponseException {
    final HttpGet getRequest = new HttpGet(new URI(url));

    final UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
    getRequest.addHeader(new BasicScheme().authenticate(creds, getRequest));
    getRequest.setHeader("Content-Type", "application/json");
    final ResponseHandler<byte[]> responseHandler = new ByteArrayResponseHandler();
    final byte[] responseBody = mClient.execute(getRequest, responseHandler);

    final File output = new File(FileConfig.TEMP_PATH + System.currentTimeMillis()+".json");
    final FileOutputStream fos = new FileOutputStream(output.getPath()); 

    fos.write(responseBody);
    fos.close();
    return output;
}

但我最近注意到了(我不知道为什么)我得到了这个例外:

01-22 07:45:51.809: E/System(9055): Uncaught exception thrown by finalizer
01-22 07:45:51.833: E/System(9055): java.io.IOException: close failed: EIO (I/O error)
01-22 07:45:51.833: E/System(9055):     at libcore.io.IoUtils.close(IoUtils.java:41)
01-22 07:45:51.833: E/System(9055):     at java.io.FileInputStream.close(FileInputStream.java:121)
01-22 07:45:51.833: E/System(9055):     at java.io.FileInputStream.finalize(FileInputStream.java:142)
01-22 07:45:51.833: E/System(9055):     at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:185)
01-22 07:45:51.833: E/System(9055):     at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
01-22 07:45:51.833: E/System(9055):     at java.lang.Thread.run(Thread.java:856)
01-22 07:45:51.833: E/System(9055): Caused by: libcore.io.ErrnoException: close failed: EIO (I/O error)
01-22 07:45:51.833: E/System(9055):     at libcore.io.Posix.close(Native Method)
01-22 07:45:51.833: E/System(9055):     at libcore.io.BlockGuardOs.close(BlockGuardOs.java:75)
01-22 07:45:51.833: E/System(9055):     at libcore.io.IoUtils.close(IoUtils.java:38)
01-22 07:45:51.833: E/System(9055):     ... 5 more
01-22 07:45:51.833: E/System(9055): Uncaught exception thrown by finalizer
01-22 07:45:51.841: E/System(9055): java.io.IOException: close failed: EIO (I/O error)
01-22 07:45:51.841: E/System(9055):     at libcore.io.IoUtils.close(IoUtils.java:41)
01-22 07:45:51.841: E/System(9055):     at java.io.FileInputStream.close(FileInputStream.java:121)
01-22 07:45:51.841: E/System(9055):     at java.io.FileInputStream.finalize(FileInputStream.java:142)
01-22 07:45:51.841: E/System(9055):     at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:185)
01-22 07:45:51.841: E/System(9055):     at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
01-22 07:45:51.841: E/System(9055):     at java.lang.Thread.run(Thread.java:856)
01-22 07:45:51.841: E/System(9055): Caused by: libcore.io.ErrnoException: close failed: EIO (I/O error)
01-22 07:45:51.841: E/System(9055):     at libcore.io.Posix.close(Native Method)
01-22 07:45:51.841: E/System(9055):     at libcore.io.BlockGuardOs.close(BlockGuardOs.java:75)
01-22 07:45:51.841: E/System(9055):     at libcore.io.IoUtils.close(IoUtils.java:38)
01-22 07:45:51.841: E/System(9055):     ... 5 more

一切似乎都有效,但我对此异常感到困惑。

targetSdk ok我的应用程序是13。

感谢您的任何评论/回应!

1 个答案:

答案 0 :(得分:2)

将代码更新为此类内容。

 public File getData(final String url) throws URISyntaxException, AuthenticationException, IOException, ClientProtocolException, HttpResponseException {
    FileOutputStream fos = null;
    File output = null;

    final HttpGet getRequest = new HttpGet(new URI(url));

    final UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
            username, password);
    getRequest.addHeader(new BasicScheme().authenticate(creds, getRequest));
    getRequest.setHeader("Content-Type", "application/json");
    final ResponseHandler<byte[]> responseHandler = new ByteArrayResponseHandler();
    final byte[] responseBody = mClient
            .execute(getRequest, responseHandler);

    try {
        output = new File(FileConfig.TEMP_PATH + System.currentTimeMillis()
                + ".json");
        fos = new FileOutputStream(output.getPath());

        fos.write(responseBody);

        fos.flush();
        fos.close();

    } catch (FileNotFoundException e) {

    } catch (IOException e) {

    } catch (Exception e) {

    } finally {
        if (fos != null) {
            fos = null;
        }
    }
    return output;
 }

因为当你尝试fos.wrire()或fos.close()时,它可能通过IOException和你调用新的FileOutputStream(output.getPath());它可能通过FileNotFoundException。