安卓StrictMode可抛出:显式的终止方法“结束”不叫 [英] Android StrictMode Throwable: Explicit termination method 'end' not called

查看:386
本文介绍了安卓StrictMode可抛出:显式的终止方法“结束”不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到一个StrictMode失败的,我看不出有什么不对我在做什么。失败是:

I'm seeing a StrictMode failure, and I can't see what's wrong with what I'm doing. The failure is:

java.lang.Throwable: Explicit termination method 'end' not called
    E/StrictMode(26875):    at dalvik.system.CloseGuard.open(CloseGuard.java:184)
    E/StrictMode(26875):    at java.util.zip.Inflater.<init>(Inflater.java:82)
    E/StrictMode(26875):    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:96)
    E/StrictMode(26875):    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:81)
    E/StrictMode(26875):    at libcore.net.http.HttpEngine.initContentStream(HttpEngine.java:528)
    E/StrictMode(26875):    at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:836)
    E/StrictMode(26875):    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
    E/StrictMode(26875):    at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:486)
    E/StrictMode(26875):    at com.mycompany.MyClass.sendJson(MyClass.java:267)

我配置StrictMode:

I configure StrictMode:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
              .detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog()
                    .penaltyDeath().build());
}

在code看起来像下面这样:

The code looks something like the following:

HttpURLConnection connection = null;
OutputStream outputStream = null;
InputStream in = null;

try {
        connection = (HttpURLConnection)new URL("http://our.server/some/path").openConnection();

        connection.setRequestProperty("Accept", "application/json");
        connection
                .setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        connection.setDoOutput(true);

        outputStream = connection.getOutputStream();

        String content = "{\"some\":\"json data\"}";
        byte bytes[] = content.getBytes("UTF-8");
        outputStream.write(bytes);
        outputStream.flush();

        responseCode = connection.getResponseCode(); // This is line 267 in the stack trace

        if (HttpURLConnection.HTTP_OK == responseCode
                || HttpURLConnection.HTTP_CREATED == responseCode) {

            // do something with a successful response
        }
    }

} catch (IOException e) {
    // report the exception
} finally {
    if (null != connection) {
        connection.disconnect();
    }
    if (null != outputStream) {
        try {
            outputStream.close();
        } catch (IOException e) {
        }
    }
    if (null != in) {
        try {
            in.close();
        } catch (IOException e) {
        }
    }
}

我发现有人看到了同样的StrictMode故障使用谷歌AdMob的SDK(<一的时候抱怨href="https://groups.google.com/forum/?fromgroups=#!topic/google-admob-ads-sdk/yVss4ufdPp4">https://groups.google.com/forum/?fromgroups=#!topic/google-admob-ads-sdk/yVss4ufdPp4)但我没能找到任何解决办法为止。

I found somebody complaining of seeing the same StrictMode failure when using Google AdMob SDK (https://groups.google.com/forum/?fromgroups=#!topic/google-admob-ads-sdk/yVss4ufdPp4) but I've not been able to find any solution so far.

推荐答案

会发生什么,如果你得到了响应流,完全阅读并关闭它(即使响应code是不正确)。
我认为这将调用 HttpEngine.release(),从而避免异常时, GzipInputStream 以后完成(如例外情况是ppared在构造函数,但扔在 $ P $敲定()

What happens if you get the response stream, read it completely and close it (even if the response code is not OK).
I think this will invoke HttpEngine.release(), and thus avoid the exception when the GzipInputStream is later finalized (as the exception is prepared in the constructor but throwed in finalize())

这篇关于安卓StrictMode可抛出:显式的终止方法“结束”不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆