Android -未捕获的异常由终结器抛出。

时间:2022-06-26 02:12:45

I have this piece of code that takes the server response and writes it into a file.

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

The file contains json data. I write response into file in order to scan json sequentially and to avoid to load big json data in a List!

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

I Think exception is thrown in this method but I'm not sure!

我认为这个方法有例外,但我不确定!

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;
}

But I've noticed that recently (I don't know why) I get this exception:

但我最近注意到(我不知道为什么)我有一个例外:

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

Everything seems to work, but I'm perplexed about this exception.

一切似乎都在起作用,但我对这个例外感到困惑。

The targetSdk ok my app is 13.

我的应用程序是13。

Thanks for any comment / response!

谢谢你的评论/回复!

1 个解决方案

#1


2  

update your code some thing like this.

像这样更新你的代码。

 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;
 }

Because when your trying to fos.wrire() or fos.close() it may through IOException and when your calling new FileOutputStream(output.getPath()); it may through FileNotFoundException.

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

#1


2  

update your code some thing like this.

像这样更新你的代码。

 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;
 }

Because when your trying to fos.wrire() or fos.close() it may through IOException and when your calling new FileOutputStream(output.getPath()); it may through FileNotFoundException.

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