我正在为一家使用MVVM和干净架构的公司构建应用程序,因此我创建了3个模块,即应用程序模块(表示层),数据模块(数据层)&域模块(域/交互器层)。现在,在我的数据模块中,我使用Retrofit和Gson自动将从登录POST请求中接收到的JSON转换为名为NetUserSession的Kotlin数据类,如下所示。我遇到的问题是日志拦截器正常打印包含数据的响应,但是response.body()
返回了一个空的NetUserSession对象,其中包含null值,这让我认为自动转换由于某种原因没有发生。有人能告诉我我在这里做错了什么吗?
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)
你知道我没看到什么吗?
1条答案
按热度按时间k7fdbhmy1#
在我绞尽脑汁几个小时之后,解决方案是简单地将模型类的成员从
val
更改为var
,如下所示: