Issue
My app works properly when there is an internet connection available. But when there's no internet, it gives a NetworkOnMainThreadException
. I'm using an AsyncTask
to run the networking code. I'm using JSoup to do some scraping.
The Logcat output is given below. Any help or tips would be appreciated.
05-14 23:52:47.258: E/AndroidRuntime(21329): FATAL EXCEPTION: main
05-14 23:52:47.258: E/AndroidRuntime(21329): android.os.NetworkOnMainThreadException
05-14 23:52:47.258: E/AndroidRuntime(21329): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1126)
05-14 23:52:47.258: E/AndroidRuntime(21329): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
05-14 23:52:47.258: E/AndroidRuntime(21329): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
05-14 23:52:47.258: E/AndroidRuntime(21329): at java.net.InetAddress.getAllByName(InetAddress.java:214)
05-14 23:52:47.258: E/AndroidRuntime(21329): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
05-14 23:52:47.258: E/AndroidRuntime(21329): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
05-14 23:52:47.258: E/AndroidRuntime(21329): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
05-14 23:52:47.258: E/AndroidRuntime(21329): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
05-14 23:52:47.258: E/AndroidRuntime(21329): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
05-14 23:52:47.258: E/AndroidRuntime(21329): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
05-14 23:52:47.258: E/AndroidRuntime(21329): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
05-14 23:52:47.258: E/AndroidRuntime(21329): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
05-14 23:52:47.258: E/AndroidRuntime(21329): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
05-14 23:52:47.258: E/AndroidRuntime(21329): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
05-14 23:52:47.258: E/AndroidRuntime(21329): at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:408)
05-14 23:52:47.258: E/AndroidRuntime(21329): at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:393)
05-14 23:52:47.258: E/AndroidRuntime(21329): at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:159)
05-14 23:52:47.258: E/AndroidRuntime(21329): at org.jsoup.helper.HttpConnection.get(HttpConnection.java:148)
05-14 23:52:47.258: E/AndroidRuntime(21329): at net.tuxv.mississaugabusses.data.Scraper.getStops(Scraper.java:60)
05-14 23:52:47.258: E/AndroidRuntime(21329): at net.tuxv.mississaugabusses.data.Route.scrapeStops(Route.java:50)
05-14 23:52:47.258: E/AndroidRuntime(21329): at net.tuxv.mississaugabusses.data.Route.getStops(Route.java:38)
05-14 23:52:47.258: E/AndroidRuntime(21329): at net.tuxv.mississaugabusses.RouteActivity$ScrapeDataTask.onPostExecute(RouteActivity.java:150)
05-14 23:52:47.258: E/AndroidRuntime(21329): at net.tuxv.mississaugabusses.RouteActivity$ScrapeDataTask.onPostExecute(RouteActivity.java:130)
05-14 23:52:47.258: E/AndroidRuntime(21329): at android.os.AsyncTask.finish(AsyncTask.java:631)
05-14 23:52:47.258: E/AndroidRuntime(21329): at android.os.AsyncTask.access$600(AsyncTask.java:177)
05-14 23:52:47.258: E/AndroidRuntime(21329): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
05-14 23:52:47.258: E/AndroidRuntime(21329): at android.os.Handler.dispatchMessage(Handler.java:99)
05-14 23:52:47.258: E/AndroidRuntime(21329): at android.os.Looper.loop(Looper.java:213)
05-14 23:52:47.258: E/AndroidRuntime(21329): at android.app.ActivityThread.main(ActivityThread.java:4787)
05-14 23:52:47.258: E/AndroidRuntime(21329): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 23:52:47.258: E/AndroidRuntime(21329): at java.lang.reflect.Method.invoke(Method.java:511)
05-14 23:52:47.258: E/AndroidRuntime(21329): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
05-14 23:52:47.258: E/AndroidRuntime(21329): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
05-14 23:52:47.258: E/AndroidRuntime(21329): at dalvik.system.NativeStart.main(Native Method)
Solution
The stack trace says the android.os.NetworkOnMainThreadException is happening from finish()
. I don't recognize that method, but is it possible you're doing networking in onPostExecute()
?
I suspect that "android.os.NetworkOnMainThreadException" means exactly what it says.
Answered By - Edward Falk
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.