Okhttp Authenticator multithreading

后端 未结 5 570
一个人的身影
一个人的身影 2020-12-18 22:11

I am using OkHttp in my android application with several async requests. All requests require a token to be sent with the header. Sometimes I need to refresh th

5条回答
  •  伪装坚强ぢ
    2020-12-18 22:26

    Add synchronized to authenticate() method signature.

    And make sure getToken() method is blocking.

    @Nullable
    @Override
    public synchronized Request authenticate(Route route, Response response) {
    
        String newAccessToken = getToken();
    
        return response.request().newBuilder()
                .header("Authorization", "Bearer " + newAccessToken)
                .build();
    }
    

提交回复
热议问题