#Why does attempting to connect to a URL crash my app?
1 messages · Page 1 of 1 (latest)
Detected code, here are some useful tools:
<@&987246623988019200> please have a look, thanks.
IOException sounds like a filesystem problem imo...
maybe there's more information in the exception, i'll try catch it
android.os.NetworkOnMainThreadException
I uploaded your attachments as Gist.
The exception is pretty self explanatory. You can't use the main thread for network operations. You can either create new thread or use coroutines.
You can either create new thread or use coroutines.
So is this similar to kotlin's "runOnUIThread", but I assume I can't run on the "UI thread", that I need to create a new thread or use a preexisting "network one"? Are any of my guesses right?
maybe runBlocking
https://hmkcode.com/android/android-network-connection-httpurlconnection-coroutine/
or maybe this
val result = withContext(Dispatchers.IO) {
// HTTP GET request code...
}
Long-running task such as CPU intensive work, writing a file to disk or network request should NOT run on the main thread. Android, for example, will throw a NetworkOnMainThreadException if you try to perform an HTTP reqeust on the main thread. To resolve this issue, we can use Kotlin coroutine to perform long-running task on a separate thread. ...
Detected code, here are some useful tools: