Поменялась структура ответа W/System.err: java.lang.NullPointerException

问题描述

应用程序正在运行,但随后响应结构和授权期间发生了一些变化登录 + 通用令牌) 未包含在应用程序中

代码

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!!)

结构访问令牌响应:

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
)

结构授权响应:

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": {
           ...
        }
    }

非常感谢您的帮助

解决方法

你必须在那里处理空异常。例如,你可以处理这样的事情。我没试过这个代码。我希望你明白这一点。

从那里开始,当您想使用此 suspend fun 时,您需要再次检查此 fun 是否为空。

if (response != null) {
     return accessToken(response?.data?.items?.getOrNull(0)?.authorizationCode!!)
}
return null