如何为错误创建拦截器以进行另一个呼叫?

时间:2019-03-13 09:02:41

标签: android kotlin retrofit okhttp

我收到401,因为令牌已过期,所以我需要通过另一个调用来更新令牌,然后再次进行调用,有没有一种简便的方法可以代替:

disposable = loginService.login(
                UserToLoginRequest(
                    input_email_login.text.toString(),
                    input_password_login.text.toString(),
                    generateRandomDeviceInfo()
                )
            )
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                    { result ->
                        //It works
                    },
                    { error -> if(error.code == 401) renewAccessToken() }
                )

问题是我想做这样的事情:Refreshing Oath token,但是如果可以用相同的参数再次调用相同的端点。

示例

getApple(1) <-返回Apple ID 1的信息

结果是401 <-如果不刷新accessToken refreshAccessToken()

就无法拨打电话

在不打扰用户的情况下自动调用getApple(1)

1 个答案:

答案 0 :(得分:0)

class Inceptor : Interceptor {
    internal var token: String?=null
    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {

        var request=chain.request()
        request=request.newBuilder().build()

        val response=chain.proceed(request)


                if (response.code() == HttpURLConnection.HTTP_UNAUTHORIZED) {
                    // get a new token (I use a synchronous Retrofit call)

                        val requestBody=FormBody.Builder()
                                .add("UserName", “abcd")
                                .add("Password", “*****")

                                .build()
                        val newRequest=request.newBuilder()
                                .url("Put your url")
                                .post(requestBody)
                                .build()
                        val tokenRefreshResponse=chain.proceed(newRequest)
                        val newRetryHttpUrl=request.url()
                        if (tokenRefreshResponse.code() == HttpURLConnection.HTTP_OK) {
                            val retryOriginaResponseBody: RequestBody

                            val builder=FormBody.Builder()
                            retryOriginaResponseBody=builder.build()
                            val retryRequest: Request
                            if (request.method() == "POST") {
                                retryRequest=request.newBuilder()
                                        .url(request.url())
                                        .post(retryOriginaResponseBody)

                                        .build()
                            } else {
                                retryRequest=request.newBuilder()
                                        .url(newRetryHttpUrl)

                                        .build()
                            }
                            val retryResponse=chain.proceed(retryRequest)
                            return retryResponse
                        } else {
                            return tokenRefreshResponse
                        }

                    }



        }
        return response
    }
}