-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
    // ...
}

this give me eroor like

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 is no DUPLICATE of this as i want simple HIT no asynctask or parsing

Community
  • 1
  • 1
Android
  • 8,995
  • 9
  • 67
  • 108
  • not using any parsing or async task – Android Apr 05 '14 at 11:54
  • You need to be using an `AsyncTask` or some background thread. You can tell that by looking at your stack trace, seeing the `NetworkOnMainThreadException`, then reading the duplicate question and its answers. The duplicate question is a duplicate because it covers the reasons why you are getting a `NetworkOnMainThreadException` and how to deal with it. You can tell this by actually reading that question and its answers. – CommonsWare Apr 05 '14 at 11:55
  • May I ask why you do not want to use AsyncTask? It is a preventive measure to prevent the UI thread freeze up if the connection goes wrong in the mid of transaction. – display name Apr 05 '14 at 12:00
  • 1
    @Pragna You need to use `AsyncTask `. Because you can't perform your Network UI operation in your `Main Thread `. – Rajendra Apr 05 '14 at 12:02
  • Also be aware that whenever you performing network related operation you must be use `Thread` or `AsyncTask`. – Rajendra Apr 05 '14 at 12:04
  • may u suggest some good link for that? – Android Apr 05 '14 at 12:05
  • You don't HAVE to do anything after you pull the URL, just create an AsyncTask, ping the URL in the `doInBackground`, and then create the AsyncTask.execute(). Simple enough. – PearsonArtPhoto Apr 05 '14 at 12:05
  • Just check these http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html , http://www.compiletimeerror.com/2013/01/why-and-how-to-use-asynctask.html#.Uz_xlaiSw6U ,http://examples.javacodegeeks.com/android/core/os/asynctask/android-asynctask-example/. These are good tutorial to understand the concepts. – Rajendra Apr 05 '14 at 12:06

1 Answers1

0

Use an AsyncTask. You can either make it a static inner class, or it's own complete class. You'll need to add try/catches and such to this, but this should work.

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;
    }
}
PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142