How to replace deprecated okhttp.RequestBody.create()
Asked Answered
T

5

24

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 ?

Tenne answered 1/11, 2019 at 14:11 Comment(0)
L
48

Just swap the parameters from

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

to

RequestBody.create(imageFile, MediaType.parse("image/*"));
Lordly answered 1/11, 2019 at 15:15 Comment(4)
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)Sublimate
why they inverted the parameters...😂Porbeagle
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.Bunin
its not working imageFile.asRequest("image/*")Monophyletic
F
8

You can use the Kotlin extensions as well.

val requestImageFile = imageFile.asRequestBody("image/*".toMediaTypeOrNull())
Farver answered 3/11, 2020 at 23:35 Comment(0)
P
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/

Parshall answered 26/9, 2020 at 17:9 Comment(0)
F
2

You can change from:

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

to:

RequestBody.Companion.create(imageFile, MediaType.parse("image/*"))
Fining answered 24/7, 2020 at 7:59 Comment(0)
B
0

I used this:

val responseBody: ResponseBody = body.toResponseBody("application/json; charset=utf-8".toMediaType())
Barneybarnhart answered 10/1 at 15:53 Comment(1)
Thank you for your interest in contributing to the Stack Overflow community. This question already has a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?Maibach

© 2022 - 2024 — McMap. All rights reserved.