• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

App Inventor Cannot invoke observer on a background thread

hi all, I'm implementing a REST Full WebService API which has only one GET method.
Inside this method I created an observer to read the <LiveData> returned by a function, but when I try to read it gives me an error: cannot invoke observe on a background thread
Here my code:

@AndroidEntryPoint
class MainActivity : AppCompatActivity()
{
private lateinit var verViewModel: VerificationViewModel
private lateinit var qrCodeText: String
var certStatus: String = "vuoto"


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


verViewModel = ViewModelProvider(this)[VerificationViewModel::class.java]

embeddedServer(Netty, 8000)
{
install(StatusPages) {
exception<Throwable> { e ->
call.respondText(e.localizedMessage, ContentType.Text.Plain, HttpStatusCode.InternalServerError)
}
}


install(ContentNegotiation)
{
gson {}
}


routing{
get("/getStatus/{qrText}") {
certStatus ="vuoto"
qrCodeText = CastNullableString(call.parameters["qrText"])
certStatus = validateGreenPass(qrCodeText)
call.respond(Response(status = certStatus))
}
}
}.start(wait=true)
}


data class Response (val status: String)

fun CastNullableString(str:String?):String
{
if (str == null)
{
return ""
}
else
{
return str
}
}


private fun validateGreenPass(qrCodeText: String):String
{
var status:String = "Vuoto"
try
{
val certObserver = Observer<CertificateSimple?> {
fun onChanged(@nullable personModel: SimplePersonModel,@nullable date:String,certificateStatus:CertificateStatus,@nullable time: Date){
status = certificateStatus.toString()
}
}
verViewModel.certificate.observe(this,certObserver)
(when execute this give me error)
verViewModel.decode(qrCodeText, false);
}
catch (e: Exception)
{
status = "ERROR: " + CastNullableString(e.message)
}
return (status)
}
}

Anyone know how to help me?
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones