Retrofit POST 响应转换失败无踪

Retrofit POST response conversion fails without a trace

我正在为一家使用 MVVM 和简洁架构的公司构建应用程序,因此我创建了 3 个模块,应用程序模块(表示层)、数据模块(数据层)和域模块(domain/interactors层)。现在,在我的数据模块中,我正在使用 Retrofit 和 Gson 自动将我从登录 POST 请求中收到的 JSON 转换为您看到的名为 NetUserSession 的 kotlin 数据 class以下。我遇到的问题是,日志记录拦截器通常打印包含数据的响应,但是 response.body() returns 一个空的 NetUserSession 对象具有空值,这让我认为自动转换不是由于某种原因发生。有人可以告诉我我做错了什么吗?

Koin模块:

val domainModule = module {
            single<LoginRepository> {LoginRepositoryImpl(get())}
            single { LoginUseCase(get()) }
        }
        val presentationModule = module {
            viewModel { LoginViewModel(get(),get()) }
        }
        val dataModule = module {
            single { ApiServiceImpl().getApiService() }
            single { LoginRepositoryImpl(get()) }
        }
}

Api 接口与改造:

interface ApiService {
    @POST("Login")
    fun getLoginResult(@Body netUser: NetUser) : Call<NetUserSession>

    @GET("Books")
    fun getBooks(@Header("Authorization") token:String) : Call<List<NetBook>>
}

class ApiServiceImpl {
    fun getApiService(): ApiService {
        val logging = HttpLoggingInterceptor()
        logging.setLevel(HttpLoggingInterceptor.Level.BODY)
        //TODO:SP Remove the interceptor code when done debugging
        val client: OkHttpClient = OkHttpClient.Builder()
            .addInterceptor(logging)
            .build()

        val retrofit = Retrofit.Builder().baseUrl(baseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .client(client)
            .build()
        // tell retrofit to implement the interface of our api
        return retrofit.create(ApiService::class.java)
    }
}

网络用户会话:

data class NetUserSession(
    @SerializedName("expires_in")
    val expires_in: Int,
    @SerializedName("token_type")
    val token_type: String,
    @SerializedName("refresh_token")
    val refresh_token: String,
    @SerializedName("access_token")
    val access_token: String
) {
    fun toUserSession(): UserSession = UserSession(
        expiresIn = expires_in,
        tokenType = token_type,
        refreshToken = refresh_token,
        accessToken = access_token
    )
}

域中的用户会话:

data class UserSession(
    val expiresIn:Int,
    val tokenType:String,
    val refreshToken:String,
    val accessToken:String
)

发生错误的LoginRepositoryImpl:

class LoginRepositoryImpl(private val apiService: ApiService) : LoginRepository {

    override suspend fun login(username:String,password:String): UserSession? = withContext(Dispatchers.IO){
        val response = apiService.getLoginResult(NetUser(username,password)).awaitResponse()
        println("THE RESPONSE WAS : ${response.body()}")
        return@withContext if(response.isSuccessful) response.body()?.toUserSession() else null
    }
}

200-OK 后的 LoggingInterceptor 结果:

{"expires_in":3600,"token_type":"Bearer","refresh_token":"T1amGR21.IdKM.5ecbf91162691e15913582bf2662e0","access_token":"T1amGT21.Idup.298885bf38e99053dca3434eb59c6aa"}

Response.body() 打印结果:

THE RESPONSE WAS : NetUserSession(expires_in=0, token_type=null, refresh_token=null, access_token=null)

有什么我在这里没看到的想法吗?

在我绞尽脑汁几个小时后,解决方案是简单地将模型 class 的成员从 val 更改为 var,如下所示:

data class NetUserSession(
    @SerializedName("expires_in")
    var expires_in: Int = 0,
    @SerializedName("token_type")
    var token_type: String? = null,
    @SerializedName("refresh_token")
    var refresh_token: String? = null,
    @SerializedName("access_token")
    var access_token: String? = null
) {
    fun toUserSession(): UserSession = UserSession(
        expiresIn = expires_in,
        tokenType = token_type!!,
        refreshToken = refresh_token!!,
        accessToken = access_token!!
    )
}