Okhttp3 - RequestBody.create(contentType, content) Deprecated

后端 未结 6 1954
清酒与你
清酒与你 2020-12-29 17:57

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          


        
6条回答
  •  Happy的楠姐
    2020-12-29 18:07

    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

提交回复
热议问题