android 2.2 中的 jsoup 问题 - 仅在第二次尝试时连接?
我的代码中遇到了一个非常奇怪的错误。在 android 2.3 上,以下代码运行得很好 - 在 logcat 中从未看到异常。然而,在 2.2 上,异常总是发生 - 但在第二次尝试时成功连接。
try {
currentTempDocument = Jsoup.connect(url).cookie("vbscansessionhash", LoginManager.getSessionValue()).get();
} catch(IOException e) {
Log.i(TAG, "Exception!", e);
try {
currentTempDocument = Jsoup.connect(url).cookie("vbscansessionhash", LoginManager.getSessionValue()).get();
} catch(IOException e2) {
}
}
我得到的异常是:
java.io.IOException: -1 error loading URL https://www.flashback.org/f4
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:387)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:396)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:364)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:143)
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:132)
at org.chip2n.flashback.HtmlReader.loadTempDocument(HtmlReader.java:69)
at org.chip2n.flashback.Browser$7.run(Browser.java:295)
如果我在 url 中将 https 切换为 http,似乎会发生同样的错误(尽管它仍然在异常中显示 https://...,但它不应该(?))。另外,当我清理我的 Eclipse 项目时,上面的代码毫无例外地工作 - 但只是在代码第一次运行时。
哦,异常立即发生 - 就像根本没有互联网连接一样。
我在这里缺少什么?
I've encountered a very weird bug in my code. On android 2.3, the following code runs just fine - never seeing the exception in logcat. However, on 2.2, the exception always happens - but manages to connect on the second try.
try {
currentTempDocument = Jsoup.connect(url).cookie("vbscansessionhash", LoginManager.getSessionValue()).get();
} catch(IOException e) {
Log.i(TAG, "Exception!", e);
try {
currentTempDocument = Jsoup.connect(url).cookie("vbscansessionhash", LoginManager.getSessionValue()).get();
} catch(IOException e2) {
}
}
The exception I'm getting is:
java.io.IOException: -1 error loading URL https://www.flashback.org/f4
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:387)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:396)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:364)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:143)
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:132)
at org.chip2n.flashback.HtmlReader.loadTempDocument(HtmlReader.java:69)
at org.chip2n.flashback.Browser$7.run(Browser.java:295)
The same exact error seem to occur if I switch https to http in the url (it still shows https://... in the exception though, which it shouldn't (?)). Also, when I clean my eclipse project, the above code works without the exception - but only the first time the code runs.
Oh, and the exception happens immediately - it's like there's no internet connection at all.
What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
似乎 JSoup 正在尝试读取以前的 HttpConnection
第一次尝试时,没有旧连接,因此代码工作正常,但第二次尝试时,它可能会尝试从过去的连接(已关闭或取消引用)读取 cookie。
我没有深入阅读 Android 2.2 代码,但对您来说最好的方法可能是继续按照您现在的方式做事。
因为它在 2.3 上工作得很好,正如它应该的那样,我很想说这是 Android 2.2 处理 cookie 的方式和 JSoup 处理 HttpConnection 的方式之间的不匹配。
It seems JSoup is trying to read cookies from a previous HttpConnection
On first try, no old connection, so the code works fine, but on second try, it may be trying to read cookies from the past connection, which is closed, or dereferenced.
I did not go as far as reading the Android 2.2 code, but probably the best way for you is to continue doing things the way you are doing right now.
Since it works fine on 2.3, as it should, I am tempted to say it's a mismatch between the way Android 2.2 handles cookies, and the way JSoup handles HttpConnection.
这个问题不仅在使用cookie时出现,在使用currentTempDocument = Jsoup.connect(url).get();做两个简单连接时也会出现这个问题
在同一活动中读取 2 个不同的 url:当我尝试读取第二个 url 时,只有第二次尝试可以。
这个问题按照评论中的建议消失了,即使用
或者也按照问题中的建议,即使用两个嵌套的 try/catch 块,以便当第一次尝试失败时,第二次尝试失败好的
This problem occurs not only using cookies, but also doing two simple connections using currentTempDocument = Jsoup.connect(url).get();
to read 2 different urls in the same activity: when I attempt to read the second url, only the 2nd attempt is ok.
This problem disappears as suggested in the comment, i.e using
or also as suggested in the question, i.e. using two nested try/catch blocks so that when the 1st attempt fails, the 2nd one goes ok