try {
URL myURL = new URL("myURL that works fine on browser");
URLConnection myURLConnection = myURL.openConnection();
myURLConnection.connect();
} catch (MalformedURLException e) {
// new URL() failed
// ...
} catch (IOException e) {
// openConnection() failed
// ...
}
这让我感觉像是
04-05 17:18:41.687: E/AndroidRuntime(11568): FATAL EXCEPTION: main
04-05 17:18:41.687: E/AndroidRuntime(11568): android.os.NetworkOnMainThreadException
04-05 17:18:41.687: E/AndroidRuntime(11568): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
04-05 17:18:41.687: E/AndroidRuntime(11568): at java.net.InetAddress.lookupHostByName(InetAddress.java:410)
04-05 17:18:41.687: E/AndroidRuntime(11568): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:241)
04-05 17:18:41.687: E/AndroidRuntime(11568): at java.net.InetAddress.getAllByName(InetAddress.java:219)
04-05 17:18:41.687: E/AndroidRuntime(11568): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
04-05 17:18:41.687: E/AndroidRuntime(11568): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
04-05 17:18:41.687: E/AndroidRuntime(11568): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
04-05 17:18:41.687: E/AndroidRuntime(11568): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
04-05 17:18:41.687: E/AndroidRuntime(11568): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
04-05 17:18:41.687: E/AndroidRuntime(11568): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
04-05 17:18:41.687: E/AndroidRuntime(11568): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
04-05 17:18:41.687: E/AndroidRuntime(11568): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
04-05 17:18:41.687: E/AndroidRuntime(11568): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
04-05 17:18:41.687: E/AndroidRuntime(11568): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
04-05 17:18:41.687: E/AndroidRuntime(11568): at com.leeway.hdwallpaper.Test_My_Paging_Activity$1.onClick(Test_My_Paging_Activity.java:89)
04-05 17:18:41.687: E/AndroidRuntime(11568): at android.view.View.performClick(View.java:4171)
04-05 17:18:41.687: E/AndroidRuntime(11568): at android.view.View$PerformClick.run(View.java:17195)
04-05 17:18:41.687: E/AndroidRuntime(11568): at android.os.Handler.handleCallback(Handler.java:643)
04-05 17:18:41.687: E/AndroidRuntime(11568): at android.os.Handler.dispatchMessage(Handler.java:92)
04-05 17:18:41.687: E/AndroidRuntime(11568): at android.os.Looper.loop(Looper.java:137)
04-05 17:18:41.687: E/AndroidRuntime(11568): at android.app.ActivityThread.main(ActivityThread.java:4803)
04-05 17:18:41.687: E/AndroidRuntime(11568): at java.lang.reflect.Method.invokeNative(Native Method)
04-05 17:18:41.687: E/AndroidRuntime(11568): at java.lang.reflect.Method.invoke(Method.java:511)
04-05 17:18:41.687: E/AndroidRuntime(11568): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
04-05 17:18:41.687: E/AndroidRuntime(11568): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
04-05 17:18:41.687: E/AndroidRuntime(11568): at dalvik.system.NativeStart.main(Native Method)
这不是this的DUPLICATE,因为我想简单的HIT没有asynctask或解析
答案 0 :(得分:0)
使用AsyncTask
。您可以将其设为静态内部类,也可以使其成为完整的类。您需要添加try
/ catch
es等,但这应该有效。
URL myURL = new URL("myURL that works fine on browser");
new UrlPinger(myUrl).execute();
class UrlPinger extends AsyncTask<URL,Void,Void> {
@Override
protected Void doInBackground (URL ... urls) {
URLConnection myURLConnection = urls[0].openConnection();
myURLConnection.connect();
return null;
}
}