5

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

该文件包含 json 数据。我将响应写入文件以便按顺序扫描 json 并避免在列表中加载大的 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。

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

4

1 回答 1

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 并且当您调用 new FileOutputStream(output.getPath()); 它可能通过 FileNotFoundException。

于 2013-01-22T07:49:00.053 回答