如何设置改造请求的优先级

时间:2018-07-02 09:48:59

标签: java android request android-viewpager retrofit

我有ViewPager,在此ViewPager中有4个片段。抱歉,我无法添加代码,因为那会很大。我创建的每个片段都向服务器发出异步改造请求。我也有access_tokenrefresh_token。因此,当我的access_token到期时,我需要使用refresh_token进行通话以更新我的access_token。因此,当我启动我的应用程序时,它将打开ViewPager,其中包含我的4个执行异步请求的片段。这意味着一次有4个请求。当我的access_token过期并且正在启动我的应用程序时,所有片段都尝试使用刷新令牌来更新访问令牌。原来,一个片段更新了access_token(在更新refresh_token更改之后),其他片段也尝试更新令牌,但是我得到401,因为1个片段已经更新了令牌,而其他片段尝试使用旧的刷新令牌来更新access_token。

这是一个请求的示例。

Call<ResponseBody> getChatImageCall = apiClient.getImages(cutMessage, CHAT_IMAGE_TYPE);
getChatImageCall.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, final Response<ResponseBody> response) {
            if (response.isSuccessful()) {
                try {
                    messagesAdapter.addToStart(MessagesFixtures.getImageBytesMessage(response.body().bytes(), id), true);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                Toast.makeText(getActivity(), "Something went wrong", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Toast.makeText(getActivity(), "An error occurred", Toast.LENGTH_SHORT).show();
        }
    });

这是令牌更新请求部分

public class RefreshToken {

Context context;
TokenUpdaterCallback tokenUpdaterCallback;
ApiClient apiClient = ApiClient.getInstance();

public RefreshToken(Context context, TokenUpdaterCallback tokenUpdaterCallback) {
    this.context = context;
    this.tokenUpdaterCallback = tokenUpdaterCallback;
}

public void updateToken() {

    //Perform this call if access token is expired

    SupportObjToken supportObjToken = new SupportObjToken();

    supportObjToken.setGrantType("refresh_token");
    supportObjToken.setClientId(SharedPreferencesManager.getInstance().getUserData().getClientId());
    supportObjToken.setClientSecret(SharedPreferencesManager.getInstance().getUserData().getClientSecret());
    supportObjToken.setRefreshToken(SharedPreferencesManager.getInstance().getUserData().getRefreshToken());

    Call<RefreshTokenActivation> newToken = apiClient.newToken(supportObjToken);
    newToken.enqueue(new Callback<RefreshTokenActivation>() {
        @Override
        public void onResponse(Call<RefreshTokenActivation> call, Response<RefreshTokenActivation> response) {
            if (response.isSuccessful()) {
                String newAccessToken = response.body().getAccessToken();
                String newRefreshToken = response.body().getRefreshToken();

                UserData userData = SharedPreferencesManager.getInstance().getUserData();
                SharedPreferencesManager.getInstance().removeUser();
                userData.setAccessToken(newAccessToken);
                userData.setRefreshToken(newRefreshToken);
                SharedPreferencesManager.getInstance().setUser(userData);

                tokenUpdaterCallback.updateToken();
            } else {
                if (response.code() == 401) {

                    //Perform this call if refresh token is expired

                    Activity activity = (Activity) context;
                    MainActivity mainActivity = (MainActivity) activity;
                    mainActivity.logOut();
                    Toast.makeText(activity, "You logged on other hardware", Toast.LENGTH_SHORT).show();
                }
            }
        }

        @Override
        public void onFailure(Call<RefreshTokenActivation> call, Throwable t) {
            Toast.makeText(context, "Response is not successful", Toast.LENGTH_SHORT).show();
        }
    });
}
}

tokenUpdaterCallback is a simple interface with updateToken() method. So you don't need this.

1 个答案:

答案 0 :(得分:1)

public class RefreshToken {

    Context context;
    TokenUpdaterCallback tokenUpdaterCallback;
    ApiClient apiClient = ApiClient.getInstance();
    boolean isLoading = false;

    public RefreshToken(Context context, TokenUpdaterCallback tokenUpdaterCallback) {
        this.context = context;
        this.tokenUpdaterCallback = tokenUpdaterCallback;
    }

    public void updateToken() {
        if (!isLoading) {
            isLoading = true;
            //Perform this call if access token is expired

            SupportObjToken supportObjToken = new SupportObjToken();

            supportObjToken.setGrantType("refresh_token");
            supportObjToken.setClientId(SharedPreferencesManager.getInstance().getUserData().getClientId());
            supportObjToken.setClientSecret(SharedPreferencesManager.getInstance().getUserData().getClientSecret());
            supportObjToken.setRefreshToken(SharedPreferencesManager.getInstance().getUserData().getRefreshToken());

            Call<RefreshTokenActivation> newToken = apiClient.newToken(supportObjToken);
            newToken.enqueue(new Callback<RefreshTokenActivation>() {
                @Override
                public void onResponse(Call<RefreshTokenActivation> call, Response<RefreshTokenActivation> response) {
                    if (response.isSuccessful()) {
                        String newAccessToken = response.body().getAccessToken();
                        String newRefreshToken = response.body().getRefreshToken();

                        UserData userData = SharedPreferencesManager.getInstance().getUserData();
                        SharedPreferencesManager.getInstance().removeUser();
                        userData.setAccessToken(newAccessToken);
                        userData.setRefreshToken(newRefreshToken);
                        SharedPreferencesManager.getInstance().setUser(userData);

                        tokenUpdaterCallback.updateToken();
                    } else {
                        if (response.code() == 401) {

                            //Perform this call if refresh token is expired

                            Activity activity = (Activity) context;
                            MainActivity mainActivity = (MainActivity) activity;
                            mainActivity.logOut();
                            Toast.makeText(activity, "You logged on other hardware", Toast.LENGTH_SHORT).show();
                        }
                        isLoading = false;
                    }
                }

                @Override
                public void onFailure(Call<RefreshTokenActivation> call, Throwable t) {
                    Toast.makeText(context, "Response is not successful", Toast.LENGTH_SHORT).show();
                    isLoading = false;
                }
            });
        }
    }
}