https://grokonez.com/android/kotlin-http-call-with-asynctask-example-android
Kotlin HTTP Call with AsyncTask example | Android
In this tutorial, we're gonna look at way to implement HTTP Call with AsyncTask
to get data from Yahoo Weather API.
I. Technologies
- Android Studio 3
- Kotlin 1.1.51
II. Overview
1. Goal
We will build an Android App that usesAsyncTask
to implement asynchronous HTTP request to Yahoo Weather API:
2. HTTP Call with AsyncTask
2.1 AsyncTask
AsyncTask allows us to perform background operations, then publishs results on the UI thread without having to manipulate threads and/or handlers.
To work with AsyncTask
, we must create its subclass, then override at least one method named doInBackground()
:
class GetWeatherAsyncTask : AsyncTask() {
override fun onPreExecute() {
// Before doInBackground
}
override fun doInBackground(vararg params: Params?): Progress {
// ...
publishProgress(progressResult)
return result
}
override fun onProgressUpdate(vararg values: Progress?) {
// use progressResult to do things such as updating UI...
}
override fun onPostExecute(result: Result?) {
// Done: use result which is returned from doInBackground()
}
}
2.2 HttpURLConnection
We use HttpURLConnection to work with HTTP-specific features.
- get a new
HttpURLConnection
by callingURL.openConnection()
- read the response from the stream returned by
getInputStream()
method (inputStream
field in Kotlin)
More at:
https://grokonez.com/android/kotlin-http-call-with-asynctask-example-android
Kotlin HTTP Call with AsyncTask example | Android
Top comments (0)