2014-03-05 6 views
1

website가 온라인 상태인지 확인하려고합니다.웹 사이트가 온라인인지 확인하십시오

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; 
    } 
} 

그러나 나는 항상 웹 사이트가 온라인 상태 인 경우에도 출력에 Error: null을 얻을 : 나는 이미 시도했다.

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)

+0

웹 사이트 순 가능 여부를 의미? 그런 식으로? – Piyush

+0

아니요, 예를 들어 google.com에 연결할 수 있는지의 여부 – ternes3

+0

스택 추적을 포함한 모든 예외 사항을 게시하십시오. –

답변

관련 문제