开发者

Android - java.net.SocketTimeoutException: Transport endpoint is not connected

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.setConnectTimeou开发者_如何学JAVAt(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();
            }
        }


It was actually missing the permission

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜