【问题标题】:Android - Uncaught exception thrown by finalizerAndroid - 终结器抛出未捕获的异常
【发布时间】:2013-01-22 15:49:00
【问题描述】:

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

该文件包含 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。

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

【问题讨论】:

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


    【解决方案1】:

    像这样更新你的代码。

     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。

    【讨论】:

    • 我已经添加了类似你的代码的东西。但即使是 catch (Exception e) 也被调用了。所以我认为这里没有引发异常。但我怎么知道哪里引发了异常?日志不包含有关我的类/包的信息.. 哇...
    • 我想我已经解决了..不是我发布的那段代码..但我在删除附加的文件之前没有关闭 JsonParser 对象..非常感谢您的回复!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    相关资源
    最近更新 更多