问题描述
我使用HttpURLConnection类进行查询后端服务器。请求是POST。我的code如下:
I'm using HttpURLConnection for querying back-end server. Requests are POST. My code looks like:
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:
However I'm getting this StrictMode error:
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)
当我调试code, os.close()
, is.close()
和 connection.disconnect()
被调用。
When I am debugging code, os.close()
, is.close()
and connection.disconnect()
is called.
能StrictMode发生,因为连接在池中保持活着?
Could StrictMode happen because of connection is keep alive in pool?
修改
- 如果
connection.setRequestProperty(连接,关闭);添加
的StrictMode错误消失 - 如果HTTP URL,而不是HTTPS URL使用StrictMode错误消失。
- 如果
的BufferedReader
使用StrictMode错误较少发生。
- If
connection.setRequestProperty("connection", "close");
is added the StrictMode error disappear. - If http URL instead of https URL is used the StrictMode error disappear.
- If
BufferedReader
is used the StrictMode error happens less often.
我想保持HTTPS安全永葆降低握手开销。
I would like to keep https for security and keep alive to reduce handshake overhead.
编辑2
看起来这仅发生在安卓3.X和4.0.X的情况。
Looks like this occurs only in case of android 3.X and 4.0.X.
推荐答案
我的code是这样的:
My code looks like this:
@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;
}
我相信你会找到答案。
I believe you'll find the answer there.
这篇关于如何正确关闭HttpURLConnection类在Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!