android 2.2 中的 jsoup 问题 - 仅在第二次尝试时连接?

发布于 2024-11-30 20:31:58 字数 1324 浏览 0 评论 0原文

我的代码中遇到了一个非常奇怪的错误。在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扎心 2024-12-07 20:31:58

似乎 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.

夏有森光若流苏 2024-12-07 20:31:58

这个问题不仅在使用cookie时出现,在使用currentTempDocument = Jsoup.connect(url).get();做两个简单连接时也会出现这个问题
在同一活动中读取 2 个不同的 url:当我尝试读取第二个 url 时,只有第二次尝试可以。
这个问题按照评论中的建议消失了,即使用

System.setProperty("http.keepAlive", "false");

或者也按照问题中的建议,即使用两个嵌套的 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

System.setProperty("http.keepAlive", "false");

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文