title: sending jpg to server with retrofit
body: <p>I'm trying to send an image to my server, i found many solutions using <strong>TypedFile</strong> but is deprecated now, i've tried using <strong>RequestBody</strong> but my <strong>JSON</strong> seems to be empty,im using <strong>Django</strong> as back-end framework and here is my <strong>retrofit</strong> code:</p>
  
  <pre><code>EndpointInterface service = ServiceAuthGenerator.createService(EndpointInterface.class);
      MediaType mediaType = MediaType.parse("multipart/form-data; boundary=---011000010111000001101001");
      RequestBody body = RequestBody.create(mediaType,file);
      //RequestBody fileBody = RequestBody.create(MediaType.parse("image/jpg"), file);
      Call&lt;JSONObject&gt; call = service.uploadPhoto(body);
      call.enqueue(new Callback&lt;JSONObject&gt;() {
  
          @Override
          public void onResponse(Response&lt;JSONObject&gt; response, Retrofit retrofit) {
              Log.v("Upload-&gt;", response.message().toString());
  
          }
  
          @Override
          public void onFailure(Throwable t) {
              Log.e("Upload", t.getMessage());
          }
      });
  }
  </code></pre>
  
  <p>can anyone tell me what im doing wrong or how i can do it? thanks! :D</p>
  
  <p>edit:</p>
  
  <p>here is my activity function:</p>
  
  <pre><code>  public void sendPhoto(File file)
      {
          EndpointInterface service = ServiceAuthGenerator.createService(EndpointInterface.class);
          RequestBody fbody = RequestBody.create(MediaType.parse("image/*"), file);
          RequestBody namebody = RequestBody.create(MediaType.parse("text/plain"), "735064");
          Call&lt;JSONObject&gt; call = service.uploadPhoto(fbody,namebody);
          call.enqueue(new Callback&lt;JSONObject&gt;() {
              @Override
              public void onResponse(Response&lt;JSONObject&gt; response, Retrofit retrofit) {
                  Log.v("Upload-&gt;", response.message().toString());
              }
              @Override
              public void onFailure(Throwable t) {
                  Log.e("Upload", t.getMessage());
              }
          });
      }
  </code></pre>
  
  <p>and this is how i set the image to my imageView:</p>
  
  <pre><code>builder.setTitle("Profile Photo");
          builder.setIcon(R.drawable.gallery2);
          builder.setAdapter(adapter,new DialogInterface.OnClickListener()
  
          {
  
              public void onClick (DialogInterface dialog,int item){
              if (item == 0) {
                  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                  /*file = new File(Environment.getExternalStorageDirectory(),
                          "tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".png");*/
                  file = new File(Environment.getExternalStorageDirectory(),"tmp_avatar.jpg");
                  mImageCaptureUri = Uri.fromFile(file);
  
                  try {
                      intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
                      intent.putExtra("return-data", true);
  
                      startActivityForResult(intent, PICK_FROM_CAMERA);
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
  
                  dialog.cancel();
              } else {
                  Intent intent = new Intent(
                          Intent.ACTION_PICK,
                          android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                  intent.setType("image/*");
                  startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
              }
          }
          }
  
          );
          final AlertDialog dialog = builder.create();
  
  
  
  
  @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          if (resultCode != RESULT_OK) return;
  
          Bitmap bitmap = null;
  
  
          if (requestCode == PICK_FROM_FILE) {
              mImageCaptureUri = data.getData();
              path = getRealPathFromURI(mImageCaptureUri); //from Gallery
  
              if (path == null)
                  path = mImageCaptureUri.getPath(); //from File Manager*/
  
              if (path != null)
                  bitmap = BitmapFactory.decodeFile(path);
          } else {
              path = mImageCaptureUri.getPath();
              bitmap = BitmapFactory.decodeFile(path);
  
          }
          final double viewWidthToBitmapWidthRatio = (double) mImageView.getWidth() / (double) bitmap.getWidth();
          mImageView.getLayoutParams().height = (int) (bitmap.getHeight() * viewWidthToBitmapWidthRatio);
  
          mImageView.setImageBitmap(bitmap);
      }
  
      public String getRealPathFromURI(Uri contentUri) {
          String[] proj = {MediaStore.Images.Media.DATA};
          Cursor cursor = managedQuery(contentUri, proj, null, null, null);
          if (cursor == null) {
  
              return null;
          }
  
          int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  
          cursor.moveToFirst();
  
  
          return cursor.getString(column_index);
      }
  </code></pre>