简体   繁体   中英

Check if website is online

I´m trying to check if a website is online. I have already tried this:

    public static boolean checkisonline(String targetUrl) {

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

    HttpURLConnection httpUrlConn;
    try {
        httpUrlConn = (HttpURLConnection) new URL(targetUrl)
                .openConnection();

        httpUrlConn.setRequestMethod("HEAD");

        // Set timeouts in milliseconds
        httpUrlConn.setConnectTimeout(30000);
        httpUrlConn.setReadTimeout(30000);

        // Print HTTP status code/message for your information.
        System.out.println("Response Code: "
                + httpUrlConn.getResponseCode());
        System.out.println("Response Message: "
                + httpUrlConn.getResponseMessage());

        return (httpUrlConn.getResponseCode() == HttpURLConnection.HTTP_OK);
    } catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
        return false;
    }
}

But I always get Error: null in the output, even if the website is online.

Stackdrace:
03-05 14:51:30.931 8409-8460/de.treevo.app I/System.out﹕ Error: null 03-05 14:51:30.931 8409-8460/de.treevo.app W/System.err﹕ java.io.EOFException 03-05 14:51:30.931 8409-8460/de.treevo.app W/System.err﹕ at java.util.zip.GZIPInputStream.readFully(GZIPInputStream.java:202) 03-05 14:51:30.931 8409-8460/de.treevo.app W/System.err﹕ at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:98) 03-05 14:51:30.931 8409-8460/de.treevo.app W/System.err﹕ at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:81) 03-05 14:51:30.931 8409-8460/de.treevo.app W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.initContentStream(HttpEngine.java:468) 03-05 14:51:30.931 8409-8460/de.treevo.app W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:666) 03-05 14:51:30.931 8409-8460/de.treevo.app W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:347) 03-05 14:51:30.931 8409-8460/de.treevo.app W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296) 03-05 14:51:30.931 8409-8460/de.treevo.app W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:503) 03-05 14:51:30.931 8409-8460/de.treevo.app W/System.err﹕ at de.treevo.core.functions.checkisonline(functions.java:79) 03-05 14:51:30.941 8409-8460/de.treevo.app W/System.err﹕ at de.treevo.app.add_series$1.run(add_series.java:167) 03-05 14:51:30.941 8409-8460/de.treevo.app W/System.err﹕ at java.lang.Thread.run(Thread.java:841)

There is a method in the java.net.InetAddress class that you could use to achieve this:

http://developer.android.com/reference/java/net/InetAddress.html#isReachable%28java.net.NetworkInterface,%20int,%20int%29

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM