应用程序正在工作,但是在响应的结构和授权期间,(登录+通用令牌)不包括在应用程序中。
代码:
suspend fun authorize(phone: String, password: String) : AccessTokenResponse? {
//safeApiCall is defined in BaseRepository.kt (https://gist.github.com/navi25/67176730f5595b3f1fb5095062a92f15)
val response = safeApiCall(
call = {api.authorize(AuthorizeRequest(phone = phone, password = password)).await()},
errorMessage = "Error Fetching Popular Movies"
)
return accessToken(response?.data?.items?.getOrNull(0)?.authorizationCode!!)
}
suspend fun accessToken(authorizationCode: String) : AccessTokenResponse? {
//safeApiCall is defined in BaseRepository.kt (https://gist.github.com/navi25/67176730f5595b3f1fb5095062a92f15)
val response = safeApiCall(
call = {api.accessToken(AccessTokenRequest(authorizationCode = authorizationCode)).await()},
errorMessage = "Error Fetching Popular Movies"
)
return response?.data?.items?.getOrNull(0)
}--即,它给出了行上的一个错误:
return accessToken(response?.data?.items?.getOrNull(0)?.authorizationCode!!)结构AccessTokenResponse:
data class AccessTokenResponse(
@SerializedName("access_token")
val accessToken: String,
@SerializedName("expires_at")
val expiresAt: Long,
@SerializedName("refresh_token")
val refreshToken: String,
@SerializedName("refresh_expires_at")
val refreshExpiresAt: Long
)结构AuthorizeResponse:
data class AuthorizeResponse(
@SerializedName("authorization_code")
val authorizationCode: String,
@SerializedName("expires_at")
val expiresAt: Long
)控制台:
W/System.err: java.lang.NullPointerException
at com.example.ferry.data.api.FerryRepository.authorize(FerryRepository.kt:29)
at com.example.ferry.data.api.FerryRepository$authorize$1.invokeSuspend(Unknown Source:12)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8512)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)json:
{
"result": true,
"errors": null,
"items": [
{
"user": [
{
...
}
],
"authorization_code": "1234352353245dsfasfasfdasf",
"expires_at": 1620982345
}
],
"versions": {
...
}
}我非常感谢你的帮助。
发布于 2021-05-18 21:47:58
您必须在那里处理空异常。例如,您可以使用这样的方法。我还没试过这个密码。我希望你明白重点。
在那里,当您想要使用这个suspend fun时,您需要再次检查这个fun是否为null。
if (response != null) {
return accessToken(response?.data?.items?.getOrNull(0)?.authorizationCode!!)
}
return nullhttps://stackoverflow.com/questions/67533000
复制相似问题