0

I try to use internet in my app (Android version 3), I add to manifest necessary permission, but this code doesnt work:

    private void qwer() throws IOException{

        DefaultHttpClient d=new DefaultHttpClient();
        HttpGet get=new HttpGet("http://google.com");
        HttpResponse r=d.execute(get);
    }

It always throw exception. I use emulator, version Android - 3.

10-07 16:23:27.744: ERROR/AndroidRuntime(3256): Caused by: android.os.NetworkOnMainThreadException
10-07 16:23:27.744: ERROR/AndroidRuntime(3256):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
10-07 16:23:27.744: ERROR/AndroidRuntime(3256):     at java.net.InetAddress.lookupHostByName(InetAddress.java:481)
10-07 16:23:27.744: ERROR/AndroidRuntime(3256):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:281)
10-07 16:23:27.744: ERROR/AndroidRuntime(3256):     at java.net.InetAddress.getAllByName(InetAddress.java:249)
10-07 16:23:27.744: ERROR/AndroidRuntime(3256):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
10-07 16:23:27.744: ERROR/AndroidRuntime(3256):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-07 16:23:27.744: ERROR/AndroidRuntime(3256):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-07 16:23:27.744: ERROR/AndroidRuntime(3256):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
10-07 16:23:27.744: ERROR/AndroidRuntime(3256):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-07 16:23:27.744: ERROR/AndroidRuntime(3256):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-07 16:23:27.744: ERROR/AndroidRuntime(3256):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-07 16:23:27.744: ERROR/AndroidRuntime(3256):     at com.dad.aaa.Qwerty11Activity.qwer(Qwerty11Activity.java:42)
10-07 16:23:27.744: ERROR/AndroidRuntime(3256):     at com.dad.aaa.Qwerty11Activity.onCreate(Qwerty11Activity.java:31)
10-07 16:23:27.744: ERROR/AndroidRuntime(3256):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
10-07 16:23:27.744: ERROR/AndroidRuntime(3256):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
user975290
  • 181
  • 2
  • 6
  • 15

1 Answers1

4

You are running the network request in the UI thread. This results in blocking the whole UI. In this case StrictMode caught it and threw you an exception to tell you that this is bad. Move the networking code into a seperate thread. A useful tool for this is the AsyncTask class.

You might also want to look into Painless Threading.

  • Implement a separate thread for a network call from Androd 4.x onwards. Use one or more of the following for threads AsyncTask, IntentService (it creates seperate worker thread), Handler. Try to avoid using java threads. – Sree Rama Jul 12 '12 at 06:05