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