我有一个返回包装结果的API,我想用一个通用包装而不是20个包装。
但是,我无法将结果值转换为通用类型。 这是代码:
根据api的响应:
override fun onResponse(call: Call, response: Response) {
val userResponse = response.getResultAs<UserResponse>()
userResponse?.let { user ->
App.shared.userSettings.edit().putString(UserSettingsKeys.ACTIVE_USER.key,
ObjectMapper().writeValueAsString(user)).apply()
App.shared.userSettings.edit().putString(UserSettingsKeys.TOKEN.key,
response.header("Authorization")).apply()
presenter?.onLoginSuccess(user)
} ?: presenter?.onLoginFailure("No response returned")
}
结果类的扩展名:
fun <T> Response.getResultAs(): T? {
this.body()?.string()?.let {
val mapper = jacksonObjectMapper()
val obj = JsonParser().parse(it).asJsonObject.toString()
val baseResponseWrapper: BaseResponseWrapper<T> = mapper.readValue(obj)
when {
baseResponseWrapper.errorValues != null -> {
baseResponseWrapper.logErrorMessage = "invalid credentials"
}
baseResponseWrapper.returnVal == null -> {
baseResponseWrapper.logErrorMessage = "returnVal could not be parsed"
}
else -> {
return baseResponseWrapper.returnVal
}
}
}
return null
}
响应包装器类:
data class BaseResponseWrapper<T>(
@field:JsonProperty("returnVal")
val returnVal: T? = null,
@field:JsonProperty("errorDescription")
val errorDescription: String? = null,
@field:JsonProperty("errorCode")
val errorCode: Int? = null,
@field:JsonProperty("errorValues")
val errorValues: Any? = null,
var logErrorMessage: String? = null
)
它将数据解析为格式正确的Linked Hash映射,而不是作为我的类。 这是我在调试崩溃之前的状态:
baseResponseWrapper.returnVal = {LinkedHashMap@7400} size = 9
0 = {LinkedHashMap$LinkedHashMapEntry@7409} "isInfluencer" -> "false"
1 = {LinkedHashMap$LinkedHashMapEntry@7410} "currentUserBranchUsername" -> "000"
2 = {LinkedHashMap$LinkedHashMapEntry@7411} "parentUser" -> " size = 4"
3 = {LinkedHashMap$LinkedHashMapEntry@7412} "profile" -> " size = 3"
4 = {LinkedHashMap$LinkedHashMapEntry@7413} "currentUserProfile" -> "null"
5 = {LinkedHashMap$LinkedHashMapEntry@7414} "email" -> "null"
6 = {LinkedHashMap$LinkedHashMapEntry@7415} "userName" -> "null"
7 = {LinkedHashMap$LinkedHashMapEntry@7416} "muviId" -> "null"
8 = {LinkedHashMap$LinkedHashMapEntry@7417} "userId" -> "0000-0000-0000"
这是我的控制台错误:
2018-10-29 12:56:15.401 1916-1962 / com.appshack.livetree E / AndroidRuntime:致命例外:OkHttp Dispatcher 流程:com.appshack.livetree,PID:1916 java.lang.IllegalStateException:已关闭 在okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:408) 在okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:402) 在okhttp3.internal.Util.bomAwareCharset(Util.java:469) 在okhttp3.ResponseBody.string(ResponseBody.java:175) 在com.appshack.livetree.utililty.extensions.OkHttpResult_getResultAsKt.getResultAs(OkHttpResult + getResultAs.kt:17) 在com.appshack.livetree.ui.outside.login.LoginInteractor $ loginResponseCallback $ 1.onResponse(LoginInteractor.kt:43) 在okhttp3.RealCall $ AsyncCall.execute(RealCall.java:153) 在okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:607) 在java.lang.Thread.run(Thread.java:761)
我不是泛型新手,但是有什么办法可以使这项工作呢?