网络主线程异常,当没有互联网时

时间:2013-05-15 03:57:33

标签: android jsoup android-networking

当有可用的互联网连接时,我的应用程序正常工作。但是当没有互联网时,它会提供NetworkOnMainThreadException。我正在使用AsyncTask来运行网络代码。我正在使用JSoup进行一些抓取。

Logcat输出如下。任何帮助或提示将不胜感激。

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)

1 个答案:

答案 0 :(得分:1)

堆栈跟踪表明android.os.NetworkOnMainThreadException发生在finish()。我不承认这种方法,但您是否有可能在onPostExecute()进行网络连接?

我怀疑“ android.os.NetworkOnMainThreadException ”意味着它所说的内容。