Okhttp3 – RequestBody.create(contentType, content) Deprecated

We Are Going To Discuss About Okhttp3 – RequestBody.create(contentType, content) Deprecated. So lets Start this Java Article.

Okhttp3 – RequestBody.create(contentType, content) Deprecated

  1. Okhttp3 – RequestBody.create(contentType, content) Deprecated

    Java Solution:
    Use create(String, MediaType) instead of create(MediaType, String) for example
    Kotlin Solution:
    Use the extension function content.toRequestBody(contentType);
    for the File type file.asRequestBody(contentType)

  2. Okhttp3 – RequestBody.create(contentType, content) Deprecated

    Java Solution:
    Use create(String, MediaType) instead of create(MediaType, String) for example
    Kotlin Solution:
    Use the extension function content.toRequestBody(contentType);
    for the File type file.asRequestBody(contentType)

Solution 1

Java Solution:
Use create(String, MediaType) instead of create(MediaType, String) for example

Kotlin Solution:
Use the extension function content.toRequestBody(contentType);
for the File type file.asRequestBody(contentType)

Note:
I’m using kotlin, but my IDE just doesn’t automatically import the class or method like import okhttp3.RequestBody.Companion.toRequestBody, so I import it manually…then use it as the example given by Saeed Younus and Pratyesh below

For more: The documentation

(In Android Studio or any Jetbrain’s IDE, the solution to the deprecated methods or class can be found by just holding the Ctrl and clicking on the create(...) of RequestBody.create)

Original Author Of This Content

Solution 2

In com.squareup.okhttp3:okhttp:4.1.0

MediaType.get("application/json; charset=utf-8") no more available.

instead this we need to use "application/json; charset=utf-8".toMediaTypeOrNull().

For example how we need to create request body now since okhttp:4.1.0

import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody

    val jsonObject = JSONObject()
        jsonObject.put("name", "Ancd test")
        jsonObject.put("city", "delhi")
        jsonObject.put("age", "23")
    val body = jsonObject.toString().toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull())

To those wondering where the answers are coming from!

All the alternatives/solutions(as described by the answer) are documented in the corresponding deprecated code! Just manoeuvre to it (the deprecated code) using whichever means your IDE supports. For example, to see the alternative/solution to the deprecated code RequestBody.create(...,...) when using AndroidStudio or any Jetbrain’s IDE, just long-press Ctrl and hover over the RequestBody.create(...,...) then click on it when it’s hovered over successfully

Original Author Of This Content

Solution 3

You need to import these files manually may be this is a bug in android studio. It is not suggested but this is work for Okhttp 4.2.2

import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.asRequestBody

and use as

val file = File("path")
file.asRequestBody("image/jpeg".toMediaTypeOrNull())

Original Author Of This Content

Solution 4

It was deprecated since version 4.0.0 of okhttp3.

The documentation for that version says

@JvmStatic
    @Deprecated(
        message = "Moved to extension function. Put the 'content' argument first to fix Java",
        replaceWith = ReplaceWith(
            expression = "content.toRequestBody(contentType)",
            imports = ["okhttp3.RequestBody.Companion.toRequestBody"]
        ),
        level = DeprecationLevel.WARNING)
    fun create(contentType: MediaType?, content: String) = content.toRequestBody(contentType)

I haven’t tried it but I believe that you should be good by doing the following:

package com.example;

import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Test {

  public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");


  OkHttpClient client = new OkHttpClient();

  public static void main(String[] args) {

  }

  String post(String url, String json) throws IOException {
    //RequestBody body = RequestBody.create(JSON, json);
    RequestBody body = RequestBody.Companion.create(json, JSON);

    Request request = new Request.Builder()
        .url(url)
        .post(body)
        .build();
    try (Response response = client.newCall(request).execute()) {
      return response.body().string();
    }
  }

}

Update: I tried to compile the file shown above using the following dependency version and RequestBody.Companion.create(json, JSON) doesn’t seem to be deprecated.

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.0.0</version>
</dependency>

Original Author Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment