title: How to modify header? retrofit2, sharedpreference
body: <p>I'm making service that makes user able to login in LoginActivity, and if login is successful, user can post something in PostActivity. I'm using Restful api.</p>
  
  <p>I found good sharedpreference example on github : <a href="https://gist.github.com/nikhiljha/52d45ca69a8415c6990d2a63f61184ff" rel="nofollow">https://gist.github.com/nikhiljha/52d45ca69a8415c6990d2a63f61184ff</a>
  and 
  <a href="https://gist.github.com/tsuharesu/cbfd8f02d46498b01f1b" rel="nofollow">https://gist.github.com/tsuharesu/cbfd8f02d46498b01f1b</a></p>
  
  <p><strong>AddCookiesInterceptor.java</strong></p>
  
  <pre><code>public class AddCookiesInterceptor implements Interceptor {
      public static final String PREF_COOKIES = "PREF_COOKIES";
  
      private Context context;
  
      public AddCookiesInterceptor(Context context) {
          this.context = context;
      }
  
      @Override
      public Response intercept(Interceptor.Chain chain) throws IOException {
          Request.Builder builder = chain.request().newBuilder();
  
          HashSet&lt;String&gt; preferences = (HashSet&lt;String&gt;) PreferenceManager.getDefaultSharedPreferences(context).getStringSet(PREF_COOKIES, new HashSet&lt;String&gt;());
  
          for (String cookie : preferences) {
            builder.addHeader("Cookie", cookie);
          }
  
          return chain.proceed(builder.build());
      }
  } 
  </code></pre>
  
  <p><strong>RecievedCookiesInterceptor.java</strong></p>
  
  <pre><code>public class ReceivedCookiesInterceptor implements Interceptor {
      private Context context;
      public ReceivedCookiesInterceptor(Context context) {
          this.context = context;
      } 
      @Override
      public Response intercept(Chain chain) throws IOException {
          Response originalResponse = chain.proceed(chain.request());
  
          if (!originalResponse.headers("Set-Cookie").isEmpty()) {
              HashSet&lt;String&gt; cookies = (HashSet&lt;String&gt;) PreferenceManager.getDefaultSharedPreferences(context).getStringSet("PREF_COOKIES", new HashSet&lt;String&gt;());
              for (String header : originalResponse.headers("Set-Cookie")) {
                  cookies.add(header);
              }
              SharedPreferences.Editor memes = PreferenceManager.getDefaultSharedPreferences(context).edit();
              memes.putStringSet("PREF_COOKIES", cookies).apply();
              memes.commit();
          }
          return originalResponse;
      }
  }
  </code></pre>
  
  <p>I used this code very well on my LoginActivity like this, </p>
  
  <pre><code>OkHttpClient client = new OkHttpClient();
  OkHttpClient.Builder builder = new OkHttpClient.Builder();
  builder.interceptors().add(new AddCookiesInterceptor(getApplicationContext()));
  builder.interceptors().add(new RecievedCookiesInterceptor(getApplicationContext()));
  client = builder.build();
  Retrofit retrofit = new Retrofit.Builder()
          .client(client)
          .addConverterFactory(GsonConverterFactory.create())
          .baseUrl(LoginApiService.Login_API_URL)
          .build();
  
  loginApiService = retrofit.create(LoginApiService.class);
  
  Call&lt;ResponseBody&gt; getkey = loginApiService.getkey(loginData);
  getkey.enqueue(new Callback&lt;ResponseBody&gt;() {
      @Override
      public void onResponse(Call&lt;ResponseBody&gt; call, Response&lt;ResponseBody&gt; response) {
          if(response.code() == 200) {
              startActivity(new Intent(LoginActivity.this, PostActivity.class));
          }
      }
  </code></pre>
  
  <p>But in PostActivity, (it's from <code>if (response.code() == 200)</code> <code>startActivity</code>)</p>
  
  <p>I used this like </p>
  
  <pre><code>    OkHttpClient client = new OkHttpClient();
      OkHttpClient.Builder builder = new OkHttpClient.Builder();
      builder.interceptors().add(new AddCookiesInterceptor(getApplicationContext()));
  #just used AddCookiesInterceptor, Not RecievedCookiesInterceptor
  
      client = builder.build();
  
      Retrofit retrofit = new Retrofit.Builder()
              .client(client)
              .addConverterFactory(GsonConverterFactory.create())
              .baseUrl(PostApiService.Post_API_URL)
              .build();
      PostApiService = retrofit.create(PostApiService.class);
  </code></pre>
  
  <p>It give me HTTP/1.1 401 error, I found it means there is problem in Headers.</p>
  
  <p>I write PostApiService like this</p>
  
  <pre><code>public interface PostApiService {
      @POST("posts/")
      Call&lt;ResponseBody&gt; gettest(@Body TextData textData);}
  </code></pre>
  
  <p><strong>My question :</strong> </p>
  
  <p>I want Header like 
  <code>key : Authorization</code> // 
  <code>value : Token e0af91707f0434a1a2a7581dd3f4f483bdd717</code></p>
  
  <p>Where do i modify?</p>
  
  <p>I know too much codes bothers you maybe, so I have to say i'm very sorry. </p>