You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

非主线程中Retrofit请求的Log.i()无法在Logcat显示的解决办法

如何让Retrofit请求的response.body()日志在Logcat中显示?

我在ViewModel中调用Retrofit请求的函数里,Log.i()语句无法在Logcat显示,即使尝试切回主线程也无效。原代码中getUserList函数的日志能输出,但显示返回的是空列表;而getUsersListRequest内的日志完全没输出。

原代码

ViewModel中的getUserList函数

//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()
        }
    }
}
// Logcat输出:inside the getUserList function of viewmodel[]

调用Retrofit的getUsersListRequest函数

//problematic logcat in the function which calls a retrofit request
suspend fun getUsersListRequest(user: User): List<User> =
    withContext(Dispatchers.IO) {
        return@withContext try {
            val response = RetrofitInstance.api.getUsers("returnUsers", user)
            Log.i("Tag", "getUserListRequest response body()" + response.body().toString())
            response.takeIf { it.isSuccessful }
                ?.body() ?: listOf()
        } catch (e: Exception) {
            listOf()
        }
    }
// Logcat无任何输出

尝试过的方案

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) { // 切换到主线程日志
                Log.i(
                    "Tag",
                    "getUserListRequest response body: ${response.body().toString()}"
                )
            }

        } catch (e: Exception) {
            errorMessage = e.message.toString()
        }
    }
}
// Logcat仍无输出

解决步骤

  • 排查请求是否异常:在getUsersListRequest的catch块中添加异常日志,确认请求是否抛出异常被吞掉:
    catch (e: Exception) {
        Log.e("Tag", "请求异常详情: ${e.message}", e) // 打印完整异常栈
        listOf()
    }
    
  • 打印请求状态码:在getUsersListRequest中先输出响应状态码,确认请求是否真的成功:
    val response = RetrofitInstance.api.getUsers("returnUsers", user)
    Log.i("Tag", "请求状态码: ${response.code()}")
    
    如果状态码不在200-299区间,说明请求未成功,不会走到response.body()的日志,直接返回空列表。
  • 检查Retrofit接口定义:确认getUsers的返回类型是否与后端响应结构匹配。比如后端返回的是单个对象而非列表,response.body()会返回null,最终返回空列表,导致上层日志显示[]。
  • 检查Logcat过滤条件:确认Logcat的Tag输入正确,日志级别未过滤Info级,且选择了当前应用的包名,避免日志被过滤。
  • 简化日志输出:直接打印原始response对象,而非仅body(),确认响应是否有内容:
    Log.i("Tag", "完整响应: $response")
    

注:Log是线程安全的,无需特意切换到主线程输出日志,之前的线程切换操作不是问题根源。

内容的提问来源于stack exchange,提问作者Deusnominus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 10:56:23