【问题标题】:addInterceptor intercept NullPointException. in Retrofit2addInterceptor 拦截器 NullPointErexception。在改造2
【发布时间】:2020-08-11 01:19:41
【问题描述】:

我收到了这个错误:

Fatal Exception: kotlin.KotlinNullPointerException
com.example.manager.helper.my_api.MyServiceGenerator$createService$$inlined$-addInterceptor$1.intercept (Interceptor.kt:81)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:100)
okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp (RealCall.kt:197)
okhttp3.internal.connection.RealCall$AsyncCall.run (RealCall.kt:502)
java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1133)
java.lang.Thread.run (Thread.java:762)

我不知道为什么会这样。

我实现的是:

        val logging = HttpLoggingInterceptor()

        if (BuildConfig.DEBUG) { // If Build is Debug Mode, Show Log
            logging.level = HttpLoggingInterceptor.Level.BODY // Logs request and response lines and their respective headers and bodies (if present).
        } else { // Otherwise, show nothing.
            logging.level = HttpLoggingInterceptor.Level.NONE // No logs
        }

        val client = OkHttpClient.Builder()
        client.addInterceptor {
            val original = it.request()

            val request = original.newBuilder()
                    .header("User-Agent", MyApp.httpUserAgent!!)
                    .build()

            it.proceed(request)
        }

        client.addInterceptor(logging)
                .connectTimeout(30, TimeUnit.SECONDS)
                .callTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)

【问题讨论】:

  • Interceptor.kt 第 81 行。如果您尝试使用 !! 将 null 转换为非 null 类型,猜测是 MyApp.httpUserAgent!! 引发的
  • @c-an 请添加您的 HttpLoggingInterceptor 的代码并标记为 81。

标签: android kotlin okhttp retrofit2.6


【解决方案1】:

您尚未完成客户端的构建。这就是为什么它是空的。通常你忘了调用 build() 方法。

这就是它必须实现的方式。

    val client = OkHttpClient.Builder()
            .addInterceptor(logging)
            .connectTimeout(30, TimeUnit.SECONDS)
            .callTimeout(30, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .writeTimeout(30, TimeUnit.SECONDS)
            .build()

【讨论】:

  • 我认为这不是问题所在。这会导致编译错误,因为client 将是类型为OkHttpClient.Builder 而不是OkHttpClient 的对象,因此OP 将无法调用client.newCall(...) 之类的方法
  • @user2340612 嗯......你是对的。我缺少特定于 kotlin 的内容。
【解决方案2】:

如果您在代码中使用!!,则应将其与空检查一起使用。像这样

if(MyApp.httpUserAgent != null) {
    val request = original.newBuilder()
                          .header("User-Agent", MyApp.httpUserAgent!!)
                          .build()
else {
    //here do something in case of null
}

【讨论】:

    【解决方案3】:

    将日志拦截器从 3.10.0 升级到 4.7.2 时遇到同样的问题

    调试发现原因是 ssl 工厂

    // old code
    builder.sslSocketFactory(sslSocketFactory)
    

    将代码更改为新代码

    // new code
    builder.sslSocketFactory(sslSocketFactory, x509TrustManager)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-08
      • 2016-11-15
      • 2016-05-02
      • 1970-01-01
      • 2014-03-27
      • 1970-01-01
      相关资源
      最近更新 更多