有互联网连接时,我的应用程序可以正常运行。但是当没有互联网时,它会给出一个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)
最佳答案
堆栈跟踪显示android.os.NetworkOnMainThreadException从finish()
发生。我无法识别该方法,但是您是否可以在onPostExecute()
中进行联网?
我怀疑“ android.os.NetworkOnMainThreadException”的含义恰恰是它所说的。