Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams "Will I need to create a separate OKHttpClient Class for this" -- usually no. See the OkHttp recipes for things like how to make POST requests. Their samples are in Java but should be fairly straightforward to translate to Kotlin. CommonsWare Jul 4, 2019 at 20:44 @HaniyehKhaksar I have referred that before asking the question. I think that doesn't say anything in particular about the post request. I am trying to make it and currently stuck here "val request = Request.Builder().url(url).post(userLoginCredentials).build()" the userLoginCredentials is a custom object and it says it needs ResponseBody() object. I one tutorial it says you can create ResponseBody Object with ResponseBody.create() but now it is depreceated Abhijith Konnayil Jul 4, 2019 at 20:52 val okHttpClient = OkHttpClient() val requestBody = payload.toRequestBody() val request = Request.Builder() .method("POST", requestBody) .url("url") .build() okHttpClient.newCall(request).enqueue(object : Callback { override fun onFailure(call: Call, e: IOException) { // Handle this override fun onResponse(call: Call, response: Response) { // Handle this

Don't forget to import:

import okhttp3.RequestBody.Companion.toRequestBody
                The import was what I was missing. I was attempting to use String.toRequestBody but it wasn't resolving.
– Jaymes Bearden
                Oct 18, 2019 at 3:39
                Version? for 3.12.1 is not working, or I just need to add something else to make it work?
– Máxima Alekz
                Apr 30, 2020 at 13:41
                @MarcoOttina now you have to create the request body with create method val body: RequestBody = RequestBody.create(JSON, refreshToken). You can see a full example at https://github.com/square/okhttp#handling-authentication
– Aenima
                Oct 1, 2020 at 9:55
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.