gpt4 book ai didi

android - 主线程上的网络异常,当没有互联网时

转载 作者:行者123 更新时间:2023-11-30 03:36:46 24 4
gpt4 key购买 nike

当有可用的互联网连接时,我的应用程序可以正常运行。但是当没有互联网时,它会给出一个 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”的意思正是它所说的。

关于android - 主线程上的网络异常,当没有互联网时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16556581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com