Quantcast
Channel: How can I fix 'android.os.NetworkOnMainThreadException'? - Stack Overflow
Viewing all articles
Browse latest Browse all 191

Answer by J.D.1731 for How do I fix 'android.os.NetworkOnMainThreadException'?

$
0
0

You can either use KOTLIN and ANKO.

Kotlin is new official language for Android more about it you can find here https://kotlinlang.org/docs/tutorials/kotlin-android.html

Anko supported library for Kotlin in Android, some doc here https://github.com/Kotlin/anko

The solution which is really useful and have only few lines of code written by @AntonioLeiva https://antonioleiva.com/anko-background-kotlin-android/

doAsync {
    var result = runLongTask()
    uiThread {
        toast(result)
    }
}

Simple as it is, NetworkOnMainThread occurs when you run background job on UI Thread so one thing you have to do is to run your longTask job in the background. You can do this using this method and Kotlin with Anko in your Android App.


Viewing all articles
Browse latest Browse all 191

Trending Articles