gpt4 book ai didi

Android 严格模式可抛出 : Explicit termination method 'end' not called

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:45:13 25 4
gpt4 key购买 nike

我看到 StrictMode 失败,但我看不出我正在做的事情有什么问题。失败是:

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)

我配置严格模式:

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

代码如下所示:

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) {
}
}
}

我发现有人提示在使用 Google AdMob SDK (https://groups.google.com/forum/?fromgroups=#!topic/google-admob-ads-sdk/yVss4ufdPp4) 时看到同样的 StrictMode 失败,但到目前为止我还没有找到任何解决方案。

最佳答案

如果您获得响应流,完全读取它并关闭它(即使响应代码不正确),会发生什么。
我认为这将调用 HttpEngine.release(),从而避免 GzipInputStream 稍后完成时的异常(因为异常是在构造函数中准备的,但在 finalize())

关于Android 严格模式可抛出 : Explicit termination method 'end' not called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15789789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com