-1
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)

不是重复的,因为我想要简单的 HIT 没有异步任务或解析

4

1 回答 1

0

使用AsyncTask. 您可以使其成为静态内部类,也可以使其成为自己的完整类。您需要为此添加try/ catches 等,但这应该可以。

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;
    }
}
于 2014-04-05T12:08:15.033 回答