Android - java.net.SocketTimeoutException:传输端点未连接

发布于 2024-11-14 23:45:08 字数 1692 浏览 2 评论 0原文

当我使用以下代码发送 GET 请求时,出现 java.net.SocketTimeoutException: Transport Endpoint is notconnected 异常。不过,此代码适用于其他 GET 请求,但不适用于某一特定 URL。知道我可能做错了什么吗?

try {
            URL mUrl = new URL(url);
            urlConn = (HttpURLConnection) mUrl.openConnection();
            urlConn.setReadTimeout(5000);
            urlConn.setConnectTimeout(5000);
            urlConn.setRequestMethod(requestMethod);
            if (contentType != null)
                urlConn.addRequestProperty("Content-Type", "application/"
                        + contentType);
            urlConn.setDoOutput(true);
            if (query != null) {
                urlConn.setRequestProperty("Content-Length",
                        Integer.toString(query.length()));
                urlConn.getOutputStream().write(query.getBytes("UTF8"));
            }
            urlConn.connect();
            if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                StringBuffer responseMsg = new StringBuffer();
                InputStream dis = urlConn.getInputStream();
                int chr;
                while ((chr = dis.read()) != -1) {
                    responseMsg.append((char) chr);
                }
                return new Response(urlConn.getResponseCode(),
                        urlConn.getResponseMessage(),
                        responseMsg.toString());
            }
            return new Response(urlConn.getResponseCode(),
                    urlConn.getResponseMessage(), null);

        } catch (IOException e) {
            throw e;
        } finally {
            if (urlConn != null) {
                urlConn.disconnect();
            }
        }

I get the java.net.SocketTimeoutException: Transport endpoint is not connected exception when I use the following piece of code to send a GET request. This code works for other GET requests though, just not for one particular URL. Any idea what I might be doing wrong?

try {
            URL mUrl = new URL(url);
            urlConn = (HttpURLConnection) mUrl.openConnection();
            urlConn.setReadTimeout(5000);
            urlConn.setConnectTimeout(5000);
            urlConn.setRequestMethod(requestMethod);
            if (contentType != null)
                urlConn.addRequestProperty("Content-Type", "application/"
                        + contentType);
            urlConn.setDoOutput(true);
            if (query != null) {
                urlConn.setRequestProperty("Content-Length",
                        Integer.toString(query.length()));
                urlConn.getOutputStream().write(query.getBytes("UTF8"));
            }
            urlConn.connect();
            if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                StringBuffer responseMsg = new StringBuffer();
                InputStream dis = urlConn.getInputStream();
                int chr;
                while ((chr = dis.read()) != -1) {
                    responseMsg.append((char) chr);
                }
                return new Response(urlConn.getResponseCode(),
                        urlConn.getResponseMessage(),
                        responseMsg.toString());
            }
            return new Response(urlConn.getResponseCode(),
                    urlConn.getResponseMessage(), null);

        } catch (IOException e) {
            throw e;
        } finally {
            if (urlConn != null) {
                urlConn.disconnect();
            }
        }

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

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

发布评论

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

评论(1

空袭的梦i 2024-11-21 23:45:08

实际上是缺少权限

<uses-permission android:name="android.permission.INTERNET" />

It was actually missing the permission

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