android kotlin - Coroutines get html from url







MainActivity.kt



package com.example.coroutine

import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.*
import java.io.IOException
import java.net.MalformedURLException
import java.net.URL


class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// make text view text scrollable
textView.movementMethod =ScrollingMovementMethod()

// url to get html
val url: URL? = try {
URL("https://developer.android.com/kotlin/")
} catch (e: MalformedURLException) {
// catch invalid url exception
button.isEnabled = false
textView.text = e.message
null
}


button.setOnClickListener {
progressBar.visibility = View.VISIBLE

// asynchronously read html from url
// io dispatcher for background thread network operation
val result = lifecycleScope.async(Dispatchers.IO) {
try {
url?.readText()
} catch (e: IOException) {
// catch exception while reading it
"Error....\n\n$e"
}
}

// main dispatcher for interacting with ui views
lifecycleScope.launch(Dispatchers.Main) {
// finally, display the html to text view when available
textView.text = result.await()

progressBar.visibility = View.INVISIBLE
}
}
}
}





activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DBD7D2"
tools:context=".MainActivity">

<com.google.android.material.button.MaterialButton
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:backgroundTint="#66023C"
android:text="Get HTML From URL"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="@+id/button"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toTopOf="@+id/button" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/textView"
style="@style/TextAppearance.MaterialComponents.Headline6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:padding="8dp"
android:fontFamily="sans-serif-thin"
android:textColor="#59260B"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button"
tools:text="TextView" />

</androidx.constraintlayout.widget.ConstraintLayout>





build.gradle (app) [code to add]



dependencies {
// kotlin coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.7'

// life cycle
def lifecycle_version = "2.2.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
}







More android kotlin tutorials