title: Retrofit2 interceptor, using token only in specific methods
body: <p>I want to use <code>authorization</code> in <code>Retrofit2</code>. I've choosen <code>interceptor</code> way because almost all methods require <code>authorization</code>, except one where I am geting token for further use with other methods.</p>
  
  <p>My question is: <strong>How can I achieve this?</strong></p>
  
  <p>I store my access token in <code>SharedPreferences</code>. My first idea was: "<em>I can retrieve it in my <code>RetrofitClient</code> class and depending if token is null or it has value create suitable <code>Retrofit</code> instance</em>" (with or without adding <code>.client()</code> to it.</p>
  
  <p>Unfortunately it's not that easy because <code>Context</code> is needed to get preferences.</p>
  
  <p>I don't know how to pass it into <code>RetrofitClient</code> class or how to get to it inside.</p>
  
  <p>Here's my <code>RetrofitClient</code> class:<br></p>
  
  <pre><code>public class RetrofitClient {
  
      public static final String BASE_URL = "http://localhost";
      public static Retrofit retrofit = null;
  
      private RetrofitClient() {
          retrofit = new Retrofit.Builder()
                  .baseUrl(BASE_URL)
                  .addConverterFactory(GsonConverterFactory.create())
                  //.client(getRequestHeader())
                  .build();
      }
  
      /*OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
          @Override
          public Response intercept(Chain chain) throws IOException {
              Request newRequest = chain.request().newBuilder()
                      .addHeader("Authorization", "Bearer" + )
          }
      })*/
  
      public static synchronized Retrofit getInstance() {
          if (retrofit == null) {
  
              retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
                      .addConverterFactory(GsonConverterFactory.create()).build();
          }
          return retrofit;
      }
  
      public Api getApi() {
          return retrofit.create(Api.class);
      }
  }
  </code></pre>
  
  <p>My <code>getRequestHeader()</code> is previous method where I was setting timeout. </p>
  
  <p>I want to create instance like this:<br></p>
  
  <pre><code>private RetrofitClient() {
              tokenService.token = null;
              tokenService.readToken(ApplicationContext.getInstance());
  
  
              if (tokenService.token == null) {
                  retrofit = new Retrofit.Builder()
                          .baseUrl(BASE_URL)
                          .addConverterFactory(GsonConverterFactory.create())
                          .build();
              } else {
                  retrofit = new Retrofit.Builder()
                          .baseUrl(BASE_URL)
                          .addConverterFactory(GsonConverterFactory.create())
                          .client(client) //I have interceptor here with access token
                          .build();
              }
          }
  </code></pre>
  
  <p>And then:<br>
  <code>RetrofitClient.getInstance().create(Api.class).getToken()</code> //getting Token<br>
  <code>RetrofitClient.getInstance().create(Api.class).someMethod()</code> //when I get Token<br><br>
  Is it good approach?</p>