Kotlin - Retrofit 接收带有对象的 json,但使用协程该对象始终显示为 NULL

Kotlin - Retrofit receives json with an object, but the object always display as NULL using Coroutines

我正在使用 Retrofit2 - Coroutines,最近我遇到了一个奇怪的问题。

我有这个协程用于用户身份验证

fun checkUserLoginCoRoutine(){
        GlobalScope.launch(Dispatchers.Main) {
            val request = repository.signIn(userRequest)
            try {
                val response = CoroutineUtil().retryIO(times = 3) { request.await() }
                when(StatusCode(response.code()).description){
                    StatusCode.Status.OK -> {
                        println("RESPONSE BODY: ${response.body()}")
                        response.body()?.let { userResponse->
                            userResponse.let {
                                println("Value of UserLoginApi: $it")
                                setPreferences(it.user.email, it.user.authenticationToken,it.preferentialPrice, it.availableCredit)
                                showResponse(true)
                                saveProductDbCoRoutine(this)
                            }
                        }
                    }
                    else -> {showResponse(false)}
                }
            }catch(e: HttpException){
                when(StatusCode(e.code()).description){
                    StatusCode.Status.NotAcceptable -> showResponse(false)
                    else -> showResponse(false)
                }
            } catch (e: Throwable){
                println("ERROR: ${e.message}")
                showServiceUnavailable()
            }
        }
    }

以及包含客户端将执行的请求和将要获得的响应的 ApiRepository。

fun signIn(userRequest: UserRequest): Deferred<Response<UserLoginApi>> {
        return apiService.sign_in(userRequest)
    }

@POST(LOGIN)
    fun sign_in(@Body user: UserRequest): Deferred<Response<UserLoginApi>>

最后是模型 Class,它将包含从 API 收到的 json 的响应。

data class UserLoginApi(@SerializedName("success")
                        val success: Boolean = false,
                        @SerializedName("user")
                        val user: User,
                        @SerializedName("preferential_price")
                        val preferentialPrice: String = "",
                        @SerializedName("available_credit")
                        val availableCredit: Double = 0.0,
                        @SerializedName("status")
                        val status: String = "")

data class User(@SerializedName("updated_at")
                val updatedAt: String = "",
                @SerializedName("activation_code")
                val activationCode: String = "",
                @SerializedName("generated")
                val generated: Boolean = false,
                @SerializedName("type_user")
                val typeUser: String = "",
                @SerializedName("authentication_token")
                val authenticationToken: String = "",
                @SerializedName("employee_id")
                val employeeId: Int? = 0,
                @SerializedName("created_at")
                val createdAt: String = "",
                @SerializedName("id")
                val id: Int = 0,
                @SerializedName("firebase_token")
                val firebaseToken: String = "",
                @SerializedName("customer_id")
                val customerId: Int = 0,
                @SerializedName("email")
                val email: String = "")

这应该有效,但是当我执行请求时,我的数据显示 user 始终是 NULL 正如您在报价。

RESPONSE BODY: UserLoginApi(success=true, user=null, preferentialPrice=p, availableCredit=0.0, status=ok)

但是在收到的 json 正文中,如果响应是 200,则表示有数据。还有 json 它表明用户确实包含数据。

D/OkHttp: {"user":{"authentication_token":"fYSPL3dQB86RLG-eDh1H","firebase_token":"dS6EbKLPG6c:APA91bHygmDw5Cf7bpVKlWLwpfF2zLmIpDGqG6poVwwf_W6tOFHWrLQCcO3Y_u6cnYOwigp5lf3PtPYdmWNsFl1HQElgX-tbD5Lfa7ys6B82wGKgh0XtQsi9h-Qt_TejYjXyzXOiudxK","id":7,"email":"86248f9238@hellomail.fun","employee_id":null,"customer_id":5,"created_at":"2019-08-01T20:06:51.000-06:00","updated_at":"2019-08-01T21:00:33.000-06:00","generated":false,"type_user":"C","activation_code":"dkeZqL8KuaTwq3tAgsUcg1Np"},"preferential_price":"p","available_credit":0.0,"success":true,"status":"ok"}

请任何人知道这可能是什么问题。这种情况突然发生,我不知道是什么原因造成的。

[[更新]]

我不知道这是否有帮助,但我已经配置 gradle 以减小 APK 的大小。

android {
    compileSdkVersion 29
    defaultConfig {
        applicationId "ni.devotion.hisabapp"
        minSdkVersion 17
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    lintOptions {
        checkReleaseBuilds false
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
    }
    signingConfigs {
        config {
            keyAlias 'LuisCB'
        }
    }
    buildTypes {
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        release {
            debuggable true
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.config
            proguardFile '/root/AndroidStudioProjects/HisabApp/app/proguard-rules.pro'
        }
    }

    flavorDimensions "default"

    productFlavors {
        development {
            dimension "default"
            applicationId "ni.devotion.hisabapp.development"
        }
        production {
            dimension "default"
            applicationId "ni.devotion.hisabapp.production"
        }
    }

    configurations {
        developmentdebug
        developmentrelease
        productiondebug
        productionrelease
        all*.exclude group: 'com.google.guava', module: 'listenablefuture'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        vectorDrawables.useSupportLibrary = true
    }

    testOptions {
        unitTests.returnDefaultValues = true
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/INDEX.LIST'
        exclude 'META-INF/lib_release.kotlin_module'
        exclude 'META-INF/library_release.kotlin_module'
    }
}

因此,问题出在混淆器中。

如果我使用 minifyEnabled

,我需要在 build.gradle 中添加这一行
-keep class ni.devotion.hisabapp.Model.**.** {*; }