SocketTimeoutException 等待但未抛出?

发布于 2025-01-02 19:12:12 字数 1489 浏览 0 评论 0原文

我正在编写一个 Android 应用程序,它从服务器接收数据。理论上不可能有互联网连接,所以我尝试通过捕获 SocketTimeoutException 来显示错误消息、重试屏幕或其他内容来捕获这种情况。不幸的是,这个异常不会被抛出。至少它不会跳入 catch 子句。我做错了什么?

public class HttpConnector {

    private String urlString;
    private int connectionTimeout = 5000; //milliseconds

    public HttpConnector(String urlString)  {
        this.urlString = urlString;
    }

    public String receiveData() throws PolizeiwarnungException {
        URL url = null;
        HttpURLConnection urlConnection = null;
        StringBuffer b = new StringBuffer();

        try {
            url = new URL(urlString);
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setReadTimeout(connectionTimeout);
            BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); //Here it gets stuck if there is no connection to the server

            String str;
            while ((str = reader.readLine()) != null) {
                b.append(str + "\n");
            }
        }
        catch (SocketTimeoutException e) {
            //TODO
            e.printStackTrace();
        }
        catch (IOException e) {
            throw new PolizeiwarnungException(e);
        } 
        finally {
            if (urlConnection != null)  {
                urlConnection.disconnect();
            }
        }

        return b.toString();
    }

    public void sendData(String data)  {
        //TODO
    }
}

I'm writing an Android app which receives data from a server. Theoretical there could not be an internet connection so I try to catch this case by catching a SocketTimeoutException to show an error message an a retry screen or something else. Unfortunately this exception won't be thrown. At least it doesn't jump into the catch clause. What am I doing wrong?

public class HttpConnector {

    private String urlString;
    private int connectionTimeout = 5000; //milliseconds

    public HttpConnector(String urlString)  {
        this.urlString = urlString;
    }

    public String receiveData() throws PolizeiwarnungException {
        URL url = null;
        HttpURLConnection urlConnection = null;
        StringBuffer b = new StringBuffer();

        try {
            url = new URL(urlString);
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setReadTimeout(connectionTimeout);
            BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); //Here it gets stuck if there is no connection to the server

            String str;
            while ((str = reader.readLine()) != null) {
                b.append(str + "\n");
            }
        }
        catch (SocketTimeoutException e) {
            //TODO
            e.printStackTrace();
        }
        catch (IOException e) {
            throw new PolizeiwarnungException(e);
        } 
        finally {
            if (urlConnection != null)  {
                urlConnection.disconnect();
            }
        }

        return b.toString();
    }

    public void sendData(String data)  {
        //TODO
    }
}

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

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

发布评论

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

评论(1

萌梦深 2025-01-09 19:12:12

您还需要设置连接超时。 请参阅此文档。

由于端点不存在,因此无需设置连接超时连接永远不会超时。

setConnectTimeout(int timeout) 设置超时值(以毫秒为单位)
用于建立与所指向的资源的连接
此 URLConnection 实例。

You need to also set the connect timeout. Please see this documentation.

Since the end point does not exist, without having set a connect time out the connection will never time out.

setConnectTimeout(int timeout) Sets the timeout value in milliseconds
for establishing the connection to the resource pointed by
this URLConnection instance.

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