title: Android Retrofit Setting Header Value Dynamically
body: <p>I have an android application in which an image is uploaded into the server. I am using retrofit library for uploading the image. For that there is a header part which is a access_token that is dynamically added when the call is made.</p>
  
  <p><strong>ApiInterface</strong></p>
  
  <pre><code>    @Headers({"source: android"})
      @Multipart
      @POST("doImageUpload.php")
      Call&lt;ResponseBody&gt; imageUpload(@Header("access_token") String accesstoken,@Part("username") RequestBody  username, @Part("action") RequestBody  action,@Part("image_data") RequestBody  image_data  );
  </code></pre>
  
  <p><strong>ImageUpload Function</strong></p>
  
  <pre><code>public void imageupload(String imagepath)
  {
  
      final ProgressDialog mProgressDialog = new ProgressDialog(this);
      mProgressDialog.setIndeterminate(true);
      mProgressDialog.setMessage("Loading...");
      mProgressDialog.show();
      String action = "imageUpload";
  
      String access = mPreferences.getString("accesstoken","");
      ApiInterface apiService =
              ApiClient.getClient().create(ApiInterface.class);
      final String TAG = "Image Upload";
  
      MediaType type = MediaType.parse("text/plain; charset=utf-8");
  
  
      Call&lt;ResponseBody&gt; call = apiService.imageUpload(@Header(RequestBody.create(type,mPreferences.getString("accesstoken", "")))
              RequestBody.create(type,mPreferences.getString("UserName", "")),RequestBody.create(type,imagepath),RequestBody.create(type,action));
  
      call.enqueue(new Callback&lt;ResponseBody&gt;() {
          @Override
          public void onResponse(Call&lt;ResponseBody&gt;call, Response&lt;ResponseBody&gt; response) {
              ResponseBody ResponseBody = response.body();
              Log.d("requrl",response.raw().request().url().toString());
              Log.d(TAG, "signup" + ResponseBody.toString());
  
  
              if (mProgressDialog.isShowing())
                  mProgressDialog.dismiss();
  
              try {
  
                  JSONObject obj = new JSONObject(response.body().string());
  
                  Log.d("chkva","--"+obj.getBoolean("status"));
  
  
                  }
  
  
              } catch (Exception exception){
  
                  Log.d("Error123","sad"+exception.getMessage());
  
              }
  
          }
  
          @Override
          public void onFailure(Call&lt;ResponseBody&gt;call, Throwable t) {
              // Log error here since request failed
              Log.e(TAG, t.toString());
          }
      });
  
  }
  </code></pre>
  
  <p>My access_token is stored in shared preference and I have to add this access_token to the image upload function.I don't know whether if I am using the correct method or not to add the header. Can anybody help me in this situation.</p>
  
  <p>Thank you in advance </p>