Android上

时间:2016-03-17 10:30:11

标签: android connection socket-timeout-exception

我有这段代码来测试与https网址的连接:

    cTimerTask = new Timer();
    cTimerTask.scheduleAtFixedRate(new TimerTask() {
        public void run() {   
try {
URL myUrl = new URL("https://www.google.it");
                            HttpsURLConnection connection = (HttpsURLConnection) myUrl.openConnection();
                            connection.setConnectTimeout(15000);
                            connection.connect();
                            Log.d(TAG, "!!! responsecode: " + connection.getResponseCode());

                            //Log.d(TAG, "connection ok");

                        } catch (MalformedURLException e) {
                            Log.d(TAG, "!!! Malformed URL");
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
     }

            }
        }, GlobalClass.DELAY_CONNECTION_CHECK, GlobalClass.PERIOD_CONNECTION_CHECK);

有时候(并非总是)有些东西是错的,我有SocketTimeoutException,但我没有连接问题,互联网运行良好。我已经使用了这段代码很长一段时间了,但在上周我遇到了这个问题。

W/System.err: java.net.SocketTimeoutException: failed to connect to www.google.it/216.58.210.195 (port 443) after 15000ms
W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
W/System.err:     at java.net.Socket.connect(Socket.java:882)
W/System.err:     at com.android.okhttp.internal.Platform.connectSocket(Platform.java:174)

你能帮我吗?

1 个答案:

答案 0 :(得分:0)

您可以直接ping IP并检查您是否收到任何回复!!