我的viewModel中有这个函数,它在另一个函数中调用 retrofit 请求,但调用 retrofit 请求的函数中的Log.i()陈述没有显示在我的logcat中.有人告诉我,这是因为它在另一个线程中运行.所以我try 返回主线程,但仍然没有显示.

这是我的viewModel和我的函数,它在我的原始代码中调用 retrofit 请求:

//viewmodel where the Log.i appears
fun getUserList(user: User) {
    viewModelScope.launch {
        try {
            userListResponse = getUsersListRequest(user)
            Log.i(
                "Tag",
                "inside the getUserList function of viewmodel" + userListResponse.toString()
            )
        } catch (e: Exception) {
            errorMessage = e.message.toString()
        }
    }
}
//result in my logcat : inside the getUserList function of viewmodel[]

//problematic logcat in the function which calls a retrofit request
suspend fun getUsersListRequest(user: User): List<User> =
    withContext(Dispatchers.IO) {
        return@withContext try {//var user:User  = user
            val response = RetrofitInstance.api.getUsers("returnUsers", user)
            Log.i("Tag", "getUserListRequest response body()" + response.body().toString())
            response.takeIf { it.isSuccessful }
                ?.body() ?: listOf()//this line is the merge of all of the class
        } catch (e: Exception) {
            listOf()
        }
    }
//nothing shows in my logcat

以下是我的解决方案try :

fun getUserList(user: User) {
    viewModelScope.launch {
        try {
            val response = RetrofitInstance.api.getUsers("returnUsers", user)
            Log.i("Tag", "getUserList in viewModel ${response.body().toString()}")
            val newList = response.takeIf { it.isSuccessful }?.body() ?: listOf()
            viewModelScope.launch { userListResponse = newList }

            withContext(Dispatchers.Main) { // Switch to main thread for logging
                Log.i(
                    "Tag",
                    "getUserListRequest response body: ${response.body().toString()}"
                ) // Log only the body
            }

        } catch (e: Exception) {
            errorMessage = e.message.toString()
        }
    }
}
//But nothing appear in my logcat

如何显示我的改装请求的respect.body()的日志(log)cat?

推荐答案

日志(log)记录不受线程影响.如果您确定没有意外过滤掉日志(log)消息,唯一的解释是Log.i实际上没有执行.

查看您提供的代码,这是完全有可能的:每当 retrofit 调用引发异常时,它就会被try/catch块吞噬并返回空列表.跳过 retrofit 呼叫的所有其他代码after.包括Log.i.如果您还将日志(log)陈述放入catch块中,则可以轻松验证这一点.两人中的一个肯定会被叫来.

Android相关问答推荐

Android Google Select 帐户总是会产生结果代码0

将动作传递给嵌套的可组合物

Android编写动画在发布版本中崩溃

Kotlin Gzip字符串未按预期工作

即使安装了Chrome和YouTube,Android对action_view a YouTube URL的意图也是空的

Android 11:在try 获取文件的永久权限后,仍然没有读写权限

Modifer.Align()不适用于行/列/框中的文本.未解决的作用域实例错误

android crashlytics 显示崩溃但不显示我的课程中的位置

SDK 33 的问题 - 面向 S+(版本 31 及更高版本)要求在创建 PendingIntent 时指定 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一

如何在 compose 中使用可变对象?

Camera2 将图像从 ImageReader 传递到 MediaRecorder

如何在我的sqlite数据库中获取某个玩家的分数

面向Jetpack Compose的可组合放置问题

Android Studio电鳗:javaHome好像无效

服务似乎在启动时忽略传递的变量

找不到(包名称).在以下位置搜索:

如何在 Android 上移动 EditText 上的图标?

Android Studio Emulator Internet 连接问题仅是第一次

在 Android Studio 中替换字符串中的 "

如何在stroke android drawable中设置渐变