0

I am reading Professional Android 2 Application Development and made a app that stands there. But when i start the avd and run the avd this pops up: Unfortunately, Earthquake has stopped. And in eclipse the logcat pop up and i get this error message:

 01-26 19:59:11.153: W/dalvikvm(859): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
01-26 19:59:11.183: E/AndroidRuntime(859): FATAL EXCEPTION: main
01-26 19:59:11.183: E/AndroidRuntime(859): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.epstudios.earthquake/com.epstudios.earthquake.Earthquake}: android.os.NetworkOnMainThreadException
01-26 19:59:11.183: E/AndroidRuntime(859):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-26 19:59:11.183: E/AndroidRuntime(859):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-26 19:59:11.183: E/AndroidRuntime(859):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-26 19:59:11.183: E/AndroidRuntime(859):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-26 19:59:11.183: E/AndroidRuntime(859):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 19:59:11.183: E/AndroidRuntime(859):  at android.os.Looper.loop(Looper.java:137)
01-26 19:59:11.183: E/AndroidRuntime(859):  at android.app.ActivityThread.main(ActivityThread.java:4745)
01-26 19:59:11.183: E/AndroidRuntime(859):  at java.lang.reflect.Method.invokeNative(Native Method)
01-26 19:59:11.183: E/AndroidRuntime(859):  at java.lang.reflect.Method.invoke(Method.java:511)
01-26 19:59:11.183: E/AndroidRuntime(859):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-26 19:59:11.183: E/AndroidRuntime(859):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-26 19:59:11.183: E/AndroidRuntime(859):  at dalvik.system.NativeStart.main(Native Method)
01-26 19:59:11.183: E/AndroidRuntime(859): Caused by: android.os.NetworkOnMainThreadException
01-26 19:59:11.183: E/AndroidRuntime(859):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
01-26 19:59:11.183: E/AndroidRuntime(859):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
01-26 19:59:11.183: E/AndroidRuntime(859):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
01-26 19:59:11.183: E/AndroidRuntime(859):  at java.net.InetAddress.getAllByName(InetAddress.java:214)
01-26 19:59:11.183: E/AndroidRuntime(859):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
01-26 19:59:11.183: E/AndroidRuntime(859):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
01-26 19:59:11.183: E/AndroidRuntime(859):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
01-26 19:59:11.183: E/AndroidRuntime(859):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
01-26 19:59:11.183: E/AndroidRuntime(859):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
01-26 19:59:11.183: E/AndroidRuntime(859):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
01-26 19:59:11.183: E/AndroidRuntime(859):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
01-26 19:59:11.183: E/AndroidRuntime(859):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
01-26 19:59:11.183: E/AndroidRuntime(859):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
01-26 19:59:11.183: E/AndroidRuntime(859):  at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
01-26 19:59:11.183: E/AndroidRuntime(859):  at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:486)
01-26 19:59:11.183: E/AndroidRuntime(859):  at com.epstudios.earthquake.Earthquake.refreshEarthquakes(Earthquake.java:71)
01-26 19:59:11.183: E/AndroidRuntime(859):  at com.epstudios.earthquake.Earthquake.onCreate(Earthquake.java:57)
01-26 19:59:11.183: E/AndroidRuntime(859):  at android.app.Activity.performCreate(Activity.java:5008)
01-26 19:59:11.183: E/AndroidRuntime(859):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-26 19:59:11.183: E/AndroidRuntime(859):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
01-26 19:59:11.183: E/AndroidRuntime(859):  ... 11 more

What is the problem

A--C
  • 36,351
  • 10
  • 106
  • 92
  • 1
    `Caused by: android.os.NetworkOnMainThreadException` Search what it means, there is a lot of answers on SO. – Alexis C. Jan 26 '14 at 20:07

2 Answers2

0

It seems the book was written for an older version of Android. It is now prohibited by default to perform networking tasks on the main thread. Restoring the policy of older Android versions can be done by calling this at the start of your app:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

StrictMode.setThreadPolicy(policy); 

Please note that you should use a background thread for networking in general.

FD_
  • 12,947
  • 4
  • 35
  • 62
-1

First line in logcat....

You cannot have network stuff on the main thread you need to asyntask wrap it

Fred Grott
  • 3,505
  • 1
  • 23
  • 18