
我正在使用httpURLConnection查询后端服务器.请求是POST.我的代码如下:
inputStream is = null;httpURLConnection connection = null;try { connection = (httpURLConnection)new URL(uri).openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=" + ENCoding); connection.setDoOutput(true); byte[] content = buildFormUrlEncoded(params); connection.setRequestProperty("Content-Length", String.valueOf(content.length)); connection.connect(); OutputStream os = null; try { os = connection.getoutputStream(); os.write(content); } finally { if (os != null) { os.close(); } } is = connection.getinputStream(); handle(is);} finally { if (is != null) { is.close(); } if (connection != null) { connection.disconnect(); }}但是我收到此StrictMode错误:
A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoIDing resource leaks.java.lang.Throwable: Explicit termination method 'close' not called at dalvik.system.CloseGuard.open(CloseGuard.java:184) at org.apache.harmony.xnet.provIDer.Jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:300) at org.apache.harmony.xnet.provIDer.Jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:257) at libcore.net.http.httpconnection.setupSecureSocket(httpconnection.java:210) at libcore.net.http.httpsURLConnectionImpl$httpsEngine.makeSslConnection(httpsURLConnectionImpl.java:477) at libcore.net.http.httpsURLConnectionImpl$httpsEngine.connect(httpsURLConnectionImpl.java:432) at libcore.net.http.httpEngine.sendSocketRequest(httpEngine.java:282) at libcore.net.http.httpEngine.sendRequest(httpEngine.java:232) at libcore.net.http.httpURLConnectionImpl.connect(httpURLConnectionImpl.java:80) at libcore.net.http.httpsURLConnectionImpl.connect(httpsURLConnectionImpl.java:164)当我调试代码时,将调用os.close(),is.close()和connection.disconnect().
由于连接在池中保持活动状态,是否会发生StrictMode?
编辑
>如果connection.setRequestProperty(“ connection”,“ close”);添加StrictMode错误消失.
>如果使用http URL而不是https URL,则StrictMode错误将消失.
>如果使用BufferedReader,则StrictMode错误的发生频率会降低.
我想保持https的安全性并保持活跃以减少握手开销.
编辑2
看起来这仅在androID 3.x和4.0.x的情况下发生.
解决方法:
我的代码如下所示:
@OverrIDe protected Boolean doInBackground(VoID... params) { JsONObject holder = new JsONObject(); try { Resources res = getResources(); holder.put(res.getString(R.string.str),""); URL url = new URL(res.getString(R.string.url)); String charset = res.getString(R.string.utf); httpURLConnection http = null; httpsURLConnection https = (httpsURLConnection) url.openConnection(); http = https; http.setRequestMethod(res.getString(R.string.post)); http.setDoinput(true); http.setDoOutput(true); http.setRequestProperty(res.getString(R.string.charset), charset); http.setRequestProperty(res.getString(R.string.content_type), "application/x-www-form-urlencoded;charset=" + charset); String query = String.format("query1=%s&query2=%s&query3=%s&query4=%s", URLEncoder.encode(res.getString(R.string.qu1), charset), URLEncoder.encode(res.getString(R.string.qu2), charset), URLEncoder.encode(res.getString(R.string.qu3), charset), URLEncoder.encode(holder.toString(), charset)); OutputStream output = null; try { output = http.getoutputStream(); output.write(query.getBytes(charset)); } finally { if (output != null) try { output.close(); } catch (IOException logorIgnore) {} } //inputStream response = http.getinputStream(); BufferedReader in = new BufferedReader(new inputStreamReader(http.getinputStream()),4800); StringBuffer responseBuffer = new StringBuffer(); String line; while ((line = in.readline()) != null) { responseBuffer.append(line); } in.close(); answer = new Gson().fromJson(responseBuffer.toString(), Answer.class); //s = responseBuffer.toString(); } catch (Exception e) { e.printstacktrace(); getData(); } return true; }我相信您会在那找到答案.
总结以上是内存溢出为你收集整理的如何在Android上正确关闭HttpURLConnection全部内容,希望文章能够帮你解决如何在Android上正确关闭HttpURLConnection所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)