私はAndroidにかなり慣れていません。私はJavaでいくつかの作業を行ったので、戻ってサイトステータスチェッカーを見つけました。
私が考えていたのは、Android は Java に基づいているため、Android アプリにコピーするだけでよいということです。
これは私のスニペットです:
public boolean checkIfSite(String url) {
HttpURLConnection connection = null;
try {
URL u = new URL(url);
connection = (HttpURLConnection) u.openConnection();
connection.setRequestMethod("HEAD");
int code = connection.getResponseCode();
System.out.println("" + code);
// You can determine on HTTP return code received. 200 is success.
return true;
} catch (MalformedURLException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
私はそれを次のように呼び出します:
if (checkIfSite("http://google.com")) {
//CODE
}
これは私が得るトレースです:
01-05 08:58:49.683: D/AndroidRuntime(9767): Shutting down VM
01-05 08:58:49.683: W/dalvikvm(9767): threadid=1: thread exiting with uncaught exception (group=0x40a211f8)
01-05 08:58:49.691: E/AndroidRuntime(9767): FATAL EXCEPTION: main
01-05 08:58:49.691: E/AndroidRuntime(9767): android.os.NetworkOnMainThreadException
01-05 08:58:49.691: E/AndroidRuntime(9767): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
01-05 08:58:49.691: E/AndroidRuntime(9767): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
01-05 08:58:49.691: E/AndroidRuntime(9767): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
01-05 08:58:49.691: E/AndroidRuntime(9767): at java.net.InetAddress.getAllByName(InetAddress.java:220)
01-05 08:58:49.691: E/AndroidRuntime(9767): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
01-05 08:58:49.691: E/AndroidRuntime(9767): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
01-05 08:58:49.691: E/AndroidRuntime(9767): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
01-05 08:58:49.691: E/AndroidRuntime(9767): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
01-05 08:58:49.691: E/AndroidRuntime(9767): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
01-05 08:58:49.691: E/AndroidRuntime(9767): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
01-05 08:58:49.691: E/AndroidRuntime(9767): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
01-05 08:58:49.691: E/AndroidRuntime(9767): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
01-05 08:58:49.691: E/AndroidRuntime(9767): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
01-05 08:58:49.691: E/AndroidRuntime(9767): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
01-05 08:58:49.691: E/AndroidRuntime(9767): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:479)
01-05 08:58:49.691: E/AndroidRuntime(9767): at com.sonyericsson.extras.liveview.plugins.sandbox.SandboxPluginService.checkIfSite(SandboxPluginService.java:184)
01-05 08:58:49.691: E/AndroidRuntime(9767): at com.sonyericsson.extras.liveview.plugins.sandbox.SandboxPluginService.button(SandboxPluginService.java:209)
01-05 08:58:49.691: E/AndroidRuntime(9767): at com.sonyericsson.extras.liveview.plugins.AbstractPluginService$LiveViewCallback$3.run(AbstractPluginService.java:200)
01-05 08:58:49.691: E/AndroidRuntime(9767): at android.os.Handler.handleCallback(Handler.java:605)
01-05 08:58:49.691: E/AndroidRuntime(9767): at android.os.Handler.dispatchMessage(Handler.java:92)
01-05 08:58:49.691: E/AndroidRuntime(9767): at android.os.Looper.loop(Looper.java:137)
01-05 08:58:49.691: E/AndroidRuntime(9767): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-05 08:58:49.691: E/AndroidRuntime(9767): at java.lang.reflect.Method.invokeNative(Native Method)
01-05 08:58:49.691: E/AndroidRuntime(9767): at java.lang.reflect.Method.invoke(Method.java:511)
01-05 08:58:49.691: E/AndroidRuntime(9767): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
01-05 08:58:49.691: E/AndroidRuntime(9767): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
01-05 08:58:49.691: E/AndroidRuntime(9767): at dalvik.system.NativeStart.main(Native Method)