Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Okhttp3 - RequestBody.create(contentType, content) Deprecated

I did not find any example of how to replace the deprecation method. The examples on the okhttp3 main page are old. This is one of them:

public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");  OkHttpClient client = new OkHttpClient();  String post(String url, String json) throws IOException {     RequestBody body = RequestBody.create(JSON, json);       Request request = new Request.Builder()           .url(url)           .post(body)           .build();   try (Response response = client.newCall(request).execute()) {     return response.body().string();   } } 

If someone could solve it, I would appreciate your help.

Update: I'm using 'com.squareup.okhttp3:okhttp:4.0.1'

like image 791
Matías Avatar asked Jul 18 '19 18:07

Matías


People also ask

How do you create a RequestBody in Java?

The RequestBody object is constructed using the builder pattern. In the below example we have added a single form parameter to the api request. RequestBody requestBody = new FormBody. Builder() .

What is Okhttpclient in Java?

OkHttp is an efficient HTTP & HTTP/2 client for Android and Java applications. It comes with advanced features, such as connection pooling (if HTTP/2 isn't available), transparent GZIP compression, and response caching, to avoid the network completely for repeated requests.

What is Mediatype parse?

parse(String string) Returns a media type for string , or null if string is not a well-formed media type. String. subtype() Returns a specific media subtype, such as "plain" or "png", "mpeg", "mp4" or "xml".


2 Answers

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)

like image 159
YuTang Avatar answered Oct 03 '22 12:10

YuTang


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

like image 33
Pratyesh Avatar answered Oct 03 '22 10:10

Pratyesh