I'm trying to get some text from my website and toast it, but I get android.os.NetworkOnMainThreadError when running
var str = URL("https://server.necrodragon41.repl.co/server/connect").readText(Charset.forName("ISO-8859-1")) Toast.makeText(applicationContext, str, Toast.LENGTH_SHORT).show() And the app crashes. Here is my code:
RegisterBtn.setOnClickListener { if (day.toString() != "0" && month.toString() != "0" && year.toString() != "0") { try { var t = Thread(Runnable { var str = URL("https://server.necrodragon41.repl.co/server/connect").readText(Charset.forName("ISO-8859-1")) Toast.makeText(applicationContext, str, Toast.LENGTH_SHORT).show() }) t.start() } catch (e: Exception) { Toast.makeText(applicationContext, e.toString(), Toast.LENGTH_SHORT).show() } } else { ErrorText.text = "The date is invalid." ErrorText.visibility = View.VISIBLE } } I tried making the thread because i got toasted the error (before putting the code into a thread). How can I get the string without crashing the app?
https://stackoverflow.com/questions/65756235/kotlin-url-readtext-crashes-application January 17, 2021 at 08:44AM
没有评论:
发表评论