Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法记录某些行_Android_Kotlin_Retrofit2_Okhttp - Fatal编程技术网

Android 无法记录某些行

Android 无法记录某些行,android,kotlin,retrofit2,okhttp,Android,Kotlin,Retrofit2,Okhttp,main活动的onCreate()包含这部分代码: val weatherService = RetrofitFactory.retrofit(AppConstants.OWM_API_URL) .create(OwmApi::class.java) GlobalScope.launch(Dispatchers.Main) { val request = weatherService.getCurrentWeatherDataByName("Ben

main活动的
onCreate()
包含这部分代码:

    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
有关,但我仍然不确定为什么即使
响应,某些日志消息也不会出现。isSuccessful
部分为true。

尝试在上设置“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
    }
}

你能在你的答案中加入代码或截图来提高它的质量吗?请添加一些代码如何设置操作,并解释为什么这样做!我应该在哪里设置此选项?你能不能给我一个代码示例,让我能更好地理解……你有什么例外吗?因为您在主线程
(Dispatchers.main)