如何在android中检查访问令牌是否已过期

时间:2018-01-11 07:06:34

标签: android oauth-2.0

我正在使用oauth2.0系统进行网络通话。我有token expire time,即:2096 sec。在我使用Volley进行任何网络通话之前。我想检查令牌是否过期。如何使用通过API提供给我的expire time来计算?

1 个答案:

答案 0 :(得分:1)

您可以尝试的是节省在共享首选项中将令牌作为“refresh_time”以及进行网络呼叫时的时间。只需使用条件检查

    float refresh_time=Calendar.getInstance().getTimeInMillis();
    saveRefreshTime(refresh_time);
    //when you refresh the token use the code written above

    float current_time=Calendar.getInstance().getTimeInMillis();


    float newTime=current_time-refresh_time/1000;

    float bufferTime=100;
    if(newTime<(2096-bufferTime)){

        //DO your network call
    }
    else{
        //refresh the token

    }

buffer_time可以是任何时间,如100秒,更安全的一面