我使用Retrofit 2.0与Jackson转换器与REST API通信.某些请求需要授权令牌.如果我拥有的令牌已过期,我需要用另一个请求刷新它们并重复上次因此而失败的请求.
我的问题:我每次都需要手动完成它还是有任何方法可以自动化它?
这是我现在实现它的方式:
TrackerService
public interface TrackerService {
@POST("auth/sendPassword")
Call<ResponseMessage> sendPassword(@Header("app-type") String appType,
@Body User userMobile);
@FormUrlEncoded
@POST("oauth/token")
Call<TokenResponse> oathToken(@Field("client_id") String clientId,
@Field("client_secret") String clientSecret,
@Field("grant_type") String grantType,
@Field("username") String username,
@Field("password") String password);
@FormUrlEncoded
@POST("oauth/token")
Call<TokenResponse> refreshToken(@Field("client_id") String clientId,
@Field("client_secret") String clientSecret,
@Field("grant_type") String grantType,
@Field("refresh_token") String username);
@PUT("me/profile")
Call<Profile> updateProfile(@Header("app-type") String appType,
@Header("Authorization") String token,
@Body Profile profile);
}
Run Code Online (Sandbox Code Playgroud)
ServiceGateway
public class ServiceGateway {
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
private …Run Code Online (Sandbox Code Playgroud)