【发布时间】:2021-08-20 13:29:05
【问题描述】:
我正在尝试通过 Volley 连接获取 api 数据并分配给 Text Composable,但它不起作用并显示错误:
@Composable 调用只能在 @Composable 函数的上下文中发生
我知道有一个类似的问题,但它没有解决我的问题
我该如何解决这个错误? 因为我是 Jetpack compose 的新手
代码:
@Composable
fun getJSON() {
val context = LocalContext.current
val queue = Volley.newRequestQueue(context)
val url = "https://adeega.xisaabso.online/android.php"
val stringRequest = StringRequest(Request.Method.POST, url,
{ response ->
val jsonObject = JSONObject(response)
val TotalMoney = jsonObject.get("Total")
Text(text = "$TotalMoney")
},
{
Toast.makeText(context, "Welcome to Error", Toast.LENGTH_SHORT).show()
})
queue.add(stringRequest)
}
设置内容
setContent {
First_JetpackCompose_appTheme {
// A surface container using the 'background' color from the theme
Surface(color = MaterialTheme.colors.background) {
getJSON()
} // END Surface
}
}
【问题讨论】:
标签: android android-studio kotlin android-jetpack-compose