Image for post
Image for post

This is my first post on Medium. I am so happy to share with you how I stopped worrying about async tasks after moving to Kotlin. I don’t want to write about how miserable it is to work on Android AsyncTasks and complication they add.

dependencies {
compile "org.jetbrains.anko:anko-commons:$anko_version"
}
doAsync{

// do background task here

uiThread{
//update UI thread after completing task
}
}
doAsync {
var
isValidUrl = false
val
cxn = url.openConnection() as HttpURLConnection
cxn.requestMethod = "GET"
cxn.useCaches = false
cxn.setRequestProperty("Cache-Control", "no-cache")
cxn.connect()
if (cxn.responseCode == HttpURLConnection.HTTP_OK) {
Log.d("Connection", "Success !" + cxn.responseCode)
isValidUrl = true
} else {
Log.d("Connection", "Fail !" + cxn.responseCode)
isValidUrl = false
}
uiThread {
if
(isValidUrl){
// do valid url stuff
}else{

}
}
}

Software engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store