call.enqueue正在阻塞第一个请求

我在Kotlin编写的Android应用程序中使用Retrofit 2来调用API。
我的电话如下所示:

val time = measureTimeMillis { val call = geocodingApi.searchByName("Berlin") call.enqueue(object : Callback { override fun onResponse(call: Call?, response: Response?) { // some code } override fun onFailure(call: Call?, t: Throwable?) { // some code } }) } Timber.d("searching by name took $time ms") 

geocodingApi在我的类的构造函数中初始化如下:

 val loggingInterceptor = HttpLoggingInterceptor(HttpLoggingInterceptor.Logger { Timber.d(it) }) loggingInterceptor.level = HttpLoggingInterceptor.Level.HEADERS val client = OkHttpClient .Builder() .addInterceptor(loggingInterceptor) .addInterceptor(GoogleApiKeyInterceptor()) .build() val retrofit = Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(ScalarsConverterFactory.create()) .addConverterFactory(MoshiConverterFactory.create(Moshi.Builder().add(KotlinJsonAdapterFactory()).build())) .client(client) .build() geocodingApi = retrofit.create(GeocodingApi::class.java) 

初始化大约需要80ms,这是可以接受的。 问题是,第一次调用阻塞UI线程需要1.5到2秒。 随后的所有呼叫都将直接返回,并在通话结束后的一段时间后调用回叫。 日志看起来像这样:

 searching by name took 1864 ms searching by name took 3 ms searching by name took 4 ms searching by name took 2 ms 

将所有调用包装在一个单独的线程中将是可能的,并产生预期的结果(没有UI阻塞),但根据文档调用call.enqueue应该在后台执行结果本身,除了第一次调用。
我做错了什么? 我正在使用Retrofit 2.3.0和OkHttp 3.8.1。

提前致谢!

问题是通过使用额外的库Kotshi和使用它来代替KotlinJsonAdapterFactory 。 Kotshi使用kapt在编译时分析注释,避免了在运行时解析注释的巨大开销。

在Retrofit Builder中,唯一必要的步骤就是替换
.addConverterFactory(MoshiConverterFactory.create(Moshi.Builder().add(KotlinJsonAdapterFactory()).build()))
通过
.addConverterFactory(MoshiConverterFactory.create(Moshi.Builder().add(ApplicationJsonAdapterFactory.INSTANCE).build()))

所有用作序列化模型的数据类都应该用@JsonSerializable进行注释。