title: upload video using Retrofit 2
body: <p>I want to upload a video file using Retrofit <strong>2</strong>, please help me doing this.</p>
  
  <p>This is my endpoint:</p>
  
  <p><strong><code>upload.php</code>:</strong> </p>
  
  <pre><code>&lt;?php
  $uploaddir = '../uploads/';
  $uploadfile = $_FILES['userfile']['name'];
  $response = array();
  if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir.$uploadfile)) 
  {
      $response["result"] = "1";
      echo json_encode($response);
  } else 
  {
      $response["result"] = "-1";
      echo json_encode($response);
  }
  ?&gt;
  </code></pre>
  
  <p><hr/>
  I tried to follow a sample (combining it with file selection from the gallery) on the web, but it doesn't even compile (I have marked the erroneous line by a comment):</p>
  
  <p><strong><code>MainActivity</code>:</strong> </p>
  
  <pre><code>public class MainActivity extends Activity 
  {
      private static int RESULT_LOAD_VIDEO = 1;
      String decodableString;
      @Override
      protected void onCreate(Bundle savedInstanceState) 
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          final Button btn_load = (Button) findViewById(R.id.buttonLoadVideo);
          btn_load.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                 loadVideoFromGallery(btn_load);
              }
          });
      }
      /*
       * PICK THE VIDEO AND EXTRACT ITS ADDRESS
       */
      public void loadVideoFromGallery(View view) 
      {
          Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                  android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
          startActivityForResult(galleryIntent, RESULT_LOAD_VIDEO);
      }
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) 
      {
          super.onActivityResult(requestCode, resultCode, data);
          try {
              // When a video is picked
              if (requestCode == RESULT_LOAD_VIDEO &amp;&amp; resultCode == RESULT_OK
                      &amp;&amp; null != data) 
              {
                  // Get the video from data
                  Uri selectedVideo = data.getData();
                  String[] filePathColumn = { MediaStore.Video.Media.DATA };
                  Cursor cursor = getContentResolver().query(selectedVideo,
                          filePathColumn, null, null, null);
                  cursor.moveToFirst();
                  int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                  decodableString = cursor.getString(columnIndex);
                  Log.i("mok",decodableString);
                  cursor.close();
                  upload(decodableString);
              } else 
              {
                  Toast.makeText(this, "You haven't picked any video",
                          Toast.LENGTH_LONG).show();
              }
          } catch (Exception e) 
          {
              e.printStackTrace();
              Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                          .show();
              }
          }
  
      /*
       * UPLOAD THE SELECTED VIDEO TO THE SRVER
       */
  
      public void upload(String decodableString)
      {
          final String BASE_URL = "http://192.168.1.7/";
          Retrofit retrofit = new Builder()
          .baseUrl(BASE_URL)
          .addConverterFactory(GsonConverterFactory.create())
          .build();
  
          UploadApiService service = retrofit.create(UploadApiService.class);
          MediaType MEDIA_TYPE = MediaType.parse("video/mp4");
          File file = new File("/storage/emulated/0/Pictures/MyApp/test.png");
          RequestBody requestBody = RequestBody.create(MEDIA_TYPE, file);
          Call&lt;com.squareup.okhttp.ResponseBody&gt; call = service.uploadVideo("desc", requestBody);
          call.enqueue(new Callback&lt;ResponseBody&gt;(){ //error even before compile***
              @Override
              public void onResponse(Response&lt;ResponseBody&gt; response, Retrofit retrofit) 
              {
                  // TODO Auto-generated method stub
                  if (response.isSuccess())
                  {
                      Log.i("mok","S");
                      ResponseBody rb = response.body();
                      Log.i("mok",rb.getUsername());
                  }
                  else
                  {
                      Log.i("mok","F");
                      com.squareup.okhttp.ResponseBody rb = response.errorBody();
                  }
              }
              @Override
              public void onFailure(Throwable t) 
              {
                  t.printStackTrace();
                  Log.i("mok",t.getCause()+"");
                  Log.i("mok","T");
                  finish();
              }
          });
      }
  }
  </code></pre>
  
  <p><strong><code>UploadApiService</code>:</strong> </p>
  
  <pre><code>public interface UploadApiService 
  {
      @Multipart
      @POST("api/upload.php")
      Call&lt;ResponseBody&gt; uploadVideo(@Part("description") String description, @Part("video") RequestBody video);
  }
  </code></pre>
  
  <p>I guess I have to use a kind of <code>Stream</code> to keep track of the progress but unfortunately I don't know more.</p>