20

I try to upload an image from an Android App to a Django server using Retrofit 2 and OkHttp3. For that, I used to create a RequestBody instance using the following lines:

RequestBody requestImageFile =
                    // NOW this call is DEPRECATED
                    RequestBody.create(
                            MediaType.parse("image/*"),

                            // a File instance created via the path string to the image
                            imageFile
                    );

I used the previous instance in the next method call as argument:

// MultipartBody.Part is used to send also the actual file name
MultipartBody.Part image = MultipartBody.Part.createFormData("image", imageFile.getName(), requestImageFile);

Finally, I fired up the Retrofit interface to do the rest:

// finally, execute the request
Call<ResponseBody> call = service.upload(image);
call.enqueue(new Callback<ResponseBody>() {
     @Override
     public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            Log.v("Upload", "success");
     }

     @Override
     public void onFailure(Call<ResponseBody> call, Throwable t) {
            Log.e("Upload error:", t.getMessage());
     }
});

Some months ago, Android Studio did not told me that create() was deprecated. When I open the project now, it tells me that create() is deprecated. Does somebody know how to fix it ?

ebeninki
  • 909
  • 1
  • 12
  • 34

4 Answers4

44

Just swap the parameters from

RequestBody.create(MediaType.parse("image/*"), imageFile);

to

RequestBody.create(imageFile, MediaType.parse("image/*"));
Shashanth
  • 4,995
  • 7
  • 41
  • 51
  • 3
    Here is also a Kotlin solution: 1. `import okhttp3.RequestBody.Companion.toRequestBody` in the import section of your Kotlin file. 2. change code to `imageFile.asRequest("image/*")` for file type, or if you have some other content (string, for example), then use `content.toRequestType(mediaType)` – Gille Jun 16 '20 at 15:35
  • 1
    why they inverted the parameters... – Vivek Thummar Mar 22 '22 at 16:33
  • This is strange/ weird ...but really cool as far as Shashanth's discovery is concerned. Thanks a lot for the workaround. it worked like a charm. – Srikumar Krishna Iyer Jan 30 '23 at 04:25
  • its not working imageFile.asRequest("image/*") – Tippu Fisal Sheriff Mar 01 '23 at 17:13
7

You can use the Kotlin extensions as well.

val requestImageFile = imageFile.asRequestBody("image/*".toMediaTypeOrNull())
sadat
  • 4,004
  • 2
  • 29
  • 49
5

Here is how to do it easily with kotlin extension functions from okhttp like: toRequestBody():

change from :

val requestImageFile = RequestBody.create(
                            MediaType.parse("image/*"),
                            imageFile
                    );

to this:

val requestImageFile = imageFile.toRequestBody(MediaType.parse("image/*"))

' more info here: https://square.github.io/okhttp/upgrading_to_okhttp_4/

Amin Keshavarzian
  • 3,646
  • 1
  • 37
  • 38
2

You can change from:

RequestBody.create(MediaType.parse("image/*"), imageFile);

to:

RequestBody.Companion.create(imageFile, MediaType.parse("image/*"))