-4

I am trying to get a web resource from my app and am getting a NetworkOnMainThreadException. When I try it from Eclipse it works fine. I have added <uses-permission android:name="android.permission.INTERNET"/> in my AndroidManifest.xml file but it's still not working. I also added -dns-server 8.8.8.8 to my Run Configurations settings but still nothing. If I open the browser app in the emulator that works fine. Any other suggestions on what could be wrong/how to fix it?

Thanks!

android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) at java.net.InetAddress.lookupHostByName(InetAddress.java:385) at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) at java.net.InetAddress.getAllByName(InetAddress.java:214) at libcore.net.http.HttpConnection.(HttpConnection.java:70) at libcore.net.http.HttpConnection.(HttpConnection.java:50) at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340) at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87) at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316) at libcore.net.http.HttpEngine.connect(HttpEngine.java:311) at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290) at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240) at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81) at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:117) at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:155) at com.weathertest.WeatherClient.getWeather(WeatherClient.java:41) at com.weathertest.MainActivity$1.onClick(MainActivity.java:44) at android.view.View.performClick(View.java:4204) at android.view.View$PerformClick.run(View.java:17355) at android.os.Handler.handleCallback(Handler.java:725) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method)

Update: Doh, I guess I didn't realize that after I added the internet permissions the exception changed. I've never seen an exception thrown for running something on the wrong thread before, makes sense that you wouldn't want to do that most of the time but, an exception, really...

As pointed out this is a good reference: How to fix android.os.NetworkOnMainThreadException?

And apparently that's the case fo Honeycomb SDK or higher, and the considerations can be found here: http://developer.android.com/training/articles/perf-anr.html

Community
  • 1
  • 1
naumcho
  • 18,671
  • 14
  • 48
  • 59
  • 1
    Either spin a new Thread, or use an AsyncTask; call WeatherClient.getWeather() from that thing. Use Activity.runOnUiThread() to pass the results back. – Seva Alekseyev Mar 29 '13 at 22:12
  • @user779: down-vote. reason: you should understand exactly from the exception name "Network on main thread exception" exactly what's the problem. it can't be clearer then that! anyway - the two answers you got are correct – Tal Kanel Mar 29 '13 at 22:33

2 Answers2

3

You must not access the internet from the main UI Thread, such as is your onCreate() method. Send your internet command from a separate Thread/Runnable or AsyncTask and your NetworkOnMainThreadException problem will be solved.

Android disallows this to make sure you don't hang the UI during long tasks by forcing you to do time consuming activity in alternate Threads.

Example:

Thread thread = new Thread()
{
    @Override
    public void run() {
        talkToTheInternet();
    }
};
thread.start();

This is just one of many possible ways to do it.

You will probably want to display a ProgressDialog and close it when the Thread finishes. There are many examples for doing that on this forum.

David Manpearl
  • 12,362
  • 8
  • 55
  • 72
1

android.os.NetworkOnMainThreadException

It is the famous Exception most of the users face when they start developing android apps.

Reason:
Android version 3.x or more devices won't allow network operation to perform in the UI Thread, when you try to do that it will throw an exception saying you are performing network operation on the UI Thread. So What you need to do is to Create a separate thread to execute network operation or AsyncTask.

Pragnani
  • 20,075
  • 6
  • 49
  • 74