Here's an example from developer.android.com
class MainActivity : AppCompatActivity() {
lateinit var textView:TextView
lateinit var button:Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView = findViewById(R.id.textView)
button = findViewById(R.id.button)
button.setOnClickListener({
getData()
})
}
fun getData(){
val queue = Volley.newRequestQueue(this)
val url = "http://www.google.com/"
val stringRequest = StringRequest(Request.Method.GET, url,
Response.Listener<String> { response ->
textView.text = "Response is: ${response.substring(0, 500)}"
},
Response.ErrorListener { textView.text = "Something went wrong!" })
queue.add(stringRequest)
}
}
How can I take advantage of coroutines so I can write my code in this manner:
val data = getData()
textView.text = data