Как забиндить suspend-функцию на кнопку?
Есть какой-то способ это сделать? Я пробовал обёртки и т. п., но не помогает. У меня то не находится launch
, то authorization
.
class AuthorizationWindow : AppCompatActivity() {
private val httpclient = HttpClient()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_authorization_window)
binder()
}
@SuppressLint("CutPasteId")
private fun binder() {
val authButton = findViewById<TextView>(R.id.winAuth_inputText_login)
authButton.setOnClickListener {
val login = findViewById<TextView>(R.id.winAuth_inputText_login).text.toString()
val password = findViewById<TextView>(R.id.winAuth_inputText_password).text.toString()
authorization() // Android Studio "Suspend function 'authorization' should be called only from a coroutine or another suspend function"
}
}
@SuppressLint("IntentWithNullActionLaunch")
suspend fun authorization(login: String, password: String) {
Log.d("TAG", "Кнопка нажата")
try {
Log.d("DEBUG", "Начало попытки")
val response: HttpResponse = httpclient.get("https://example.com/auth/?login=${login}&password=${password}")
Log.d("DEBUG", "Конец попытки")
} catch (_: IOException) {
Log.d("DEBUG", "Поймал ошибку")
findViewById<TextView>(R.id.status).text = "Сервер не ответил"
Log.d("DEBUG", "Обработал ошибку")
}
val intent = Intent(this, MainWindow::class.java)
startActivity(intent)
}
override fun onDestroy() {
super.onDestroy()
httpclient.close()
}
}
Источник: Stack Overflow на русском