TokenInterceptor.java 722 B

12345678910111213141516171819202122232425
  1. package com.jyc.threegames.net;
  2. import android.text.TextUtils;
  3. import com.jyc.threegames.controller.LoginController;
  4. import java.io.IOException;
  5. import okhttp3.Interceptor;
  6. import okhttp3.Request;
  7. import okhttp3.Response;
  8. public class TokenInterceptor implements Interceptor {
  9. @Override
  10. public Response intercept(Chain chain) throws IOException {
  11. String token = LoginController.getInstance().getToken();
  12. if (TextUtils.isEmpty(token))
  13. return chain.proceed(chain.request());
  14. Request request = chain.request()
  15. .newBuilder()
  16. .addHeader("Authorization", "Bearer " + token)
  17. .build();
  18. return chain.proceed(request);
  19. }
  20. }