gpt4 book ai didi

java - 关闭 Jsoup 连接

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:05:38 29 4
gpt4 key购买 nike

我需要一些帮助来了解 Jsoup 的基础知识。以下代码有效,但我想知道是否需要以某种方式关闭连接。在 Jsoup 网站上找不到任何关于它的信息。如果应用程序在后台方法执行后保持不变,我每五分钟左右在日志 cat 中收到一条消息,说“请求时间失败:java.net.SocketException:协议(protocol)不支持的地址族”。所以我想确保我没有不必要地消耗数据。谢谢。

            protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try {
// connect to web page based on user input
Document doc = Jsoup.connect(routeURL).get();


// select relevant page elements
Elements fareStageNumbers = doc.getElementsByClass("fare_stages_inner_table");

// test printing out fare stage numbers
for(Element div : fareStageNumbers){

Log.v(TAG, div.text());

}



} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

日志消息:

    01-12 20:58:28.755: D/SntpClient(78): request time failed: java.net.SocketException: Address family not supported by protocol
01-12 21:03:28.765: D/SntpClient(78): request time failed: java.net.SocketException: Address family not supported by protocol
01-12 21:08:28.775: D/SntpClient(78): request time failed: java.net.SocketException: Address family not supported by protocol

最佳答案

Jsoup 在请求完成后自行关闭连接:

// from 'org.jsoup.helper.HttpConnection' class
static HttpConnection.Response execute(Connection.Request req, HttpConnection.Response previousResponse) throws IOException {
// ...
HttpURLConnection conn = createConnection(req);
HttpConnection.Response res;
try {
// ...
} finally {
// per Java's documentation, this is not necessary, and precludes keepalives. However in practise,
// connection errors will not be released quickly enough and can cause a too many open files error.
conn.disconnect();
}
// ...
}

异常:您的网址是否包含协议(protocol)(网址以例如 http:// 开头)?

关于java - 关闭 Jsoup 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14302886/

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