2

当我使用以下代码发送 GET 请求时,我得到 java.net.SocketTimeoutException: Transport endpoint is not connected 异常。不过,此代码适用于其他 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();
            }
        }
4

1 回答 1

5

它实际上缺少许可

<uses-permission android:name="android.permission.INTERNET" />
于 2011-07-26T15:31:22.617 回答