Android (2.1) HttpGet.abort() 不起作用

发布于 2024-12-02 22:09:33 字数 285 浏览 1 评论 0原文

我调用 abort() ,并且在execute() 中阻塞的线程不会解除阻塞,直到因异常而超时。可能需要 10 秒以上,所以这是一个大问题。

我环顾四周,找不到这个问题的答案。

文档说 abort() 应该导致阻塞 i/o 的execute() 立即解除阻塞。研究表明这个问题不久前已经得到解决。

当然,我从不同的线程调用 abort() 和execute()。我的代码没有什么特别的,所以我不会浪费空间列出它。

我发现一些帖子建议在 HttpClient 下搞乱套接字等,但这些都不起作用。

I call abort() and the thread that is blocking in execute() doesn't unblock until it times out with an exception. It can take over 10 seconds, so this is a big problem.

I've looked around for a while and I can't find the answer to this.

The docs say that abort() should cause the execute() blocking i/o to unblock immediately. Research suggests that this problem was fixed a while ago.

I am calling abort() and execute() from different threads, of course. There's nothing special about my code, so I won't waste space listing it.

Some posts I found suggested messing with the sockets and such underneath the HttpClient, but none of those worked either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

醉态萌生 2024-12-09 22:09:33

您是否只是希望 HttpGet 在已知时间后超时,或者是否有理由必须尝试取消获取?我怀疑中止只有在有要读取的响应流之后才起作用......就像中止长下载一样。您可以尝试使用 URLConnection 而不是 HttpGet,它允许您独立设置读取和连接超时。

URL url= new URL(urlString);
        URLConnection conn = url.openConnection();
        conn.setConnectTimeout(getInstance().getConnectionTimeout());
        conn.setReadTimeout(getInstance().getReadTimeout());
        if (encodedPostargs != null)
        {
            conn.setDoOutput(true);
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
            out.write(encodedPostargs);
            out.flush();
            out.close();
        }
        return new BufferedReader(new InputStreamReader(conn.getInputStream()));

Are you just wanting HttpGet to timeout after a known amount of time, or is there a reason why you must try to cancel the get? I suspect that abort only works after it has a response stream to be reading...as in abort a long download. You might try using URLConnection instead of HttpGet which allows you to set read and connection timeouts independently.

URL url= new URL(urlString);
        URLConnection conn = url.openConnection();
        conn.setConnectTimeout(getInstance().getConnectionTimeout());
        conn.setReadTimeout(getInstance().getReadTimeout());
        if (encodedPostargs != null)
        {
            conn.setDoOutput(true);
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
            out.write(encodedPostargs);
            out.flush();
            out.close();
        }
        return new BufferedReader(new InputStreamReader(conn.getInputStream()));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文