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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…