无法记录某些行

Unable to Log certain lines

我的onCreate() of MainActivity包含这部分代码:

    val weatherService = RetrofitFactory.retrofit(AppConstants.OWM_API_URL)
        .create(OwmApi::class.java)
    GlobalScope.launch(Dispatchers.Main) {
        val request = weatherService.getCurrentWeatherDataByName("Bengaluru", "metric")
        Log.i("Response 0", 0.toString())
        try {
            val response = request.await()
            if(response.isSuccessful) {
                Log.i("Response 2", 2.toString())
                val weatherResponse = response.body()
                Log.i("Response 3", 3.toString())
                Log.i("Response", weatherResponse.toString())
                println(weatherResponse)
            }
            else {
                Log.i("Response 5", 5.toString())
                Log.d(TAG, response.errorBody().toString())
            }
        }
        catch (e: Exception) {
            e.stackTrace
        }
    }

weatherService的客户端包含一个拦截器,其定义如下:

private val loggingInterceptor =  HttpLoggingInterceptor().apply {
    level = HttpLoggingInterceptor.Level.BODY
}

现在,当我 运行 我的应用程序时,我只看到带有 Response 0 标记的日志,但看不到 Response,甚至看不到 else 部分当我将查询参数更改为无效值时,我的代码。也许这与 loggingInterceptor 有关,但我仍然不确定为什么尽管 response.isSuccessful 部分为真,但某些日志消息没有出现。

尝试在

上设置 "No filter" 选项

您必须在后台线程中调用 api 调用,可能会出现异常。

所以像下面这样尝试

 val weatherService = RetrofitFactory.retrofit(AppConstants.OWM_API_URL)
    .create(OwmApi::class.java)
GlobalScope.launch(Dispatchers.IO) {
    val request = weatherService.getCurrentWeatherDataByName("Bengaluru", "metric")
    Log.i("Response 0", 0.toString())
    try {
        val response = request.await()
        if(response.isSuccessful) {
            Log.i("Response 2", 2.toString())
            val weatherResponse = response.body()
            Log.i("Response 3", 3.toString())
            Log.i("Response", weatherResponse.toString())
            println(weatherResponse)
        }
        else {
            Log.i("Response 5", 5.toString())
            Log.d(TAG, response.errorBody().toString())
        }
    }
    catch (e: Exception) {
        e.stackTrace
    }
}