Kotlin, Retrofit, ClassCastException, com.google.gson.internal.LinkedTreeMap 无法转换为 List<CatListItem>?

问题描述 投票:0回答:1

我的界面

interface ApiService {
    @GET("URL")
    fun getCatList(
        @Query("var1") var1: String,
        @Query("var2") var2: String,
    ): Call<List<CatListItem>?>
}

我的帮手

class ApiClient {
    companion object {
        private lateinit var retrofit: Retrofit

        fun getClient(): Retrofit {
            val interceptor = HttpLoggingInterceptor()
            interceptor.level = HttpLoggingInterceptor.Level.BODY

            val client = OkHttpClient.Builder().addInterceptor(interceptor).build()

            retrofit = Retrofit.Builder()
                .baseUrl(ApiConstants.baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build()

            return retrofit
        }
    }
}

我的应用程序

    override fun getCatList(var1: String, var2: String) {
        val apiInterface: ApiService = ApiClient.getClient().create(ApiService::class.java)

        val call = apiInterface.getCatList(var1, var2)

        call.enqueue(object : Callback<List<CatListItem>?> {
            override fun onResponse(
                call: Call<List<CatListItem>?>, response: Response<List<CatListItem>?>
            ) {
                if (response.isSuccessful) {
                    val answer = CatList(response.body())
                    return
                }
            }

            override fun onFailure(call: Call<List<CatListItem>?>, t: Throwable) {
                call.cancel()
            }
        })
    }

我的数据类

data class CatList(

    @field:SerializedName("catList")
    val catList: List<CatListItem?>? = null
)

data class CatListItem(

    @field:SerializedName("name")
    val name: String? = null,
)

我的 JSON

[{"name":"1"},{"name":"2"}]

我的 ProGuard 规则

-keep,allowobfuscation,allowshrinking interface retrofit2.Call
-keep,allowobfuscation,allowshrinking class retrofit2.Response
-keep,allowobfuscation,allowshrinking class retrofit2.*
-keep,allowobfuscation,allowshrinking class kotlin.coroutines.Continuation

-keep class com.google.gson.** { *; }
-keep class com.google.gson.reflect.** { *; }
-keep class com.google.gson.stream.** { *; }
-keep class com.google.gson.internal.** { *; }

-keep class * implements java.io.Serializable { *; }

-keepclassmembers class * {
    @com.google.gson.annotations.SerializedName <fields>;
}

-keepclassmembers enum * {
    *;
}

Release 模式下的 Logcat,除 Release 模式外在 Debug 模式下一切正常

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to q2.a
                                                                                                        at o2.c.run(Unknown Source:385)
                                                                                                        at java.util.TimerThread.mainLoop(Timer.java:562)
                                                                                                        at java.util.TimerThread.run(Timer.java:512)

问题出在APK Release模式下,Debug模式下一切正常

我改变了超时,但没有任何改变。还在界面中测试了一个带有 suspend fun 的 runBlocking,没有任何改变。好像少了什么。我怀疑这是在内部处理列表时发生的?

需要你的帮助来解决这个问题。在此先感谢您的支持。

kotlin casting gson retrofit classcastexception
1个回答
0
投票

好的,谢谢你的回答。

我的问题的解决方案是: - 评论我的乐趣 getCatList(var1: String, var2: String)。 -编译发布。 - 然后取消注释我的功能。 - 再次编译。

不知道为什么这是这个问题的解决方案。

但对我有用。

如果有人能告诉我为什么会这样,将帮助我理解这个项目中是否遗漏了什么。

稍后。

© www.soinside.com 2019 - 2024. All rights reserved.