如何处理 URLConnection 的连接超时错误(Java、Android)

发布于 2025-01-11 20:21:43 字数 552 浏览 2 评论 0原文

我有以下代码:

        try {
            url = new URL(sURL);
            URLConnection connection  = url.openConnection();
            connection.setConnectTimeout(1000);
            connection.setReadTimeout(2000);
            InputStream inputStream = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));

<从阅读器读取...> 捕获(IOException e){ e.printStackTrace(); 如何

检测连接超时问题?我想将此事件作为特殊情况来处理。 IOException 对于我的目的来说是通用的。

有什么帮助吗?

I have the following code:

        try {
            url = new URL(sURL);
            URLConnection connection  = url.openConnection();
            connection.setConnectTimeout(1000);
            connection.setReadTimeout(2000);
            InputStream inputStream = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));

<Read from reader ...>
catch (IOException e) {
e.printStackTrace();
}

How can I detect connection-timeout issue? I would like to handle this event as a special case.
IOException is to general for my purpose.

Any help?

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

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

发布评论

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

评论(1

所谓喜欢 2025-01-18 20:21:43
catch(java.net.SocketTimeoutException sox)
    {
        sox.printStackTrace();
    }
catch(java.net.SocketTimeoutException sox)
    {
        sox.printStackTrace();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文