尝试在Retrofit服务中将数据类用作结果类型时收到以下错误。
11-15 11:35:38.345 14693-14693/com.example.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.app, PID: 14693
kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Incorrect resolution sequence for Java constructor public constructor AuthenticationResponse() defined in com.example.app.data.auth.AuthenticationResponse[JavaClassConstructorDescriptor@6d57dc3] (kotlin.reflect.jvm.internal.impl.load.java.structure.reflect.ReflectJavaClass: class com.example.app.data.auth.AuthenticationResponse)
at kotlin.reflect.jvm.internal.RuntimeTypeMapper.mapSignature(RuntimeTypeMapper.kt:202)
at kotlin.reflect.jvm.internal.KFunctionImpl.<init>(KFunctionImpl.kt:46)
at kotlin.reflect.jvm.internal.KClassImpl$Data$constructors$2.invoke(KClassImpl.kt:87)
at kotlin.reflect.jvm.internal.KClassImpl$Data$constructors$2.invoke(KClassImpl.kt:39)
at kotlin.reflect.jvm.internal.ReflectProperties$LazySoftVal.invoke(ReflectProperties.java:93)
at kotlin.reflect.jvm.internal.ReflectProperties$Val.getValue(ReflectProperties.java:32)
at kotlin.reflect.jvm.internal.KClassImpl$Data.getConstructors(Unknown Source:7)
at kotlin.reflect.jvm.internal.KClassImpl.getConstructors(KClassImpl.kt:192)
at kotlin.reflect.full.KClasses.getPrimaryConstructor(KClasses.kt:40)
at com.squareup.moshi.KotlinJsonAdapterFactory.create(KotlinJsonAdapter.kt:160)
at com.squareup.moshi.Moshi.adapter(Moshi.java:100)
at retrofit2.converter.moshi.MoshiConverterFactory.responseBodyConverter(MoshiConverterFactory.java:90)
at retrofit2.Retrofit.nextResponseBodyConverter(Retrofit.java:328)
at retrofit2.Retrofit.responseBodyConverter(Retrofit.java:311)
at retrofit2.ServiceMethod$Builder.createResponseConverter(ServiceMethod.java:735)
at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:168)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:169)
at retrofit2.Retrofit$1.invoke(Retrofit.java:146)
at java.lang.reflect.Proxy.invoke(Proxy.java:913)
at $Proxy4.credentialsLogin(Unknown Source)
at com.example.app.data.rest.service.AuthenticationService$DefaultImpls.credentialsLogin$default(AuthenticationService.kt:37)
at com.example.app.ui.module.auth.AuthenticatorActivity.login(AuthenticatorActivity.kt:204)
at com.example.app.ui.module.auth.AuthenticatorActivity.access$login(AuthenticatorActivity.kt:37)
at com.example.app.ui.module.auth.AuthenticatorActivity$onCreate$4.onEditorAction(AuthenticatorActivity.kt:107)
at android.widget.TextView.onEditorAction(TextView.java:5909)
at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:138)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:360)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:85)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
说实话,我甚至不确定这个错误意味着什么,而且我还没有找到很多关于它的资源。 class description相当含糊:
表示Kotlin反射已达到不一致状态 它无法恢复。
唯一使用反射的地方是在Moshi内,但我无法弄清楚是什么导致了这个问题。
这是数据类:
data class AuthenticationResponse(
@Json(name = "access_token) val accessToken: String,
@Json(name = "refresh_token) val refreshToken: String,
@Json(name = "user_id") val userId: String,
val scope: String
)
AuthenticationService
:
interface AuthenticationService {
@FormUrlEncoded
@POST("oauth/token")
fun credentials(
@Field("username") username: String,
@Field("password") password: String,
@Field("scope") scope: String,
@Field("grant_type") grantType: String = "password"
): Flowable<AuthenticationResponse>
@FormUrlEncoded
@POST("oauth/token")
fun refreshToken(
@Field("refresh_token") refreshToken: String,
@Field("scope") scope: String,
@Field("grant_type") grantType: String = "refresh_token"
): Flowable<AuthenticationResponse>
}
我使用Moshi作为JSON转换器,我已将KotlinJsonAdapterFactory
包含在Moshi.Builder
中。
我可以通过尝试直接使用Moshi创建AuthenticationResponse
适配器来可靠地重现错误。
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
val json = "{\"user_id\": 000000," +
"\"access_token\": \"testAccessToken\"," +
"\"expires_in\": 3600," +
"\"token_type\": \"Bearer\"," +
"\"refresh_token\": \"testRefreshToken\"," +
"\"scope\": \"test\"}"
val adapter = moshi.adapter<AuthenticationResponse>(AuthenticationResponse::class.java)
val response = adapter.fromJson(json)
Timber.d("response=$response")
尝试创建KotlinReflectionInternalError
值时,上面的代码失败并显示相同的adapter
。