Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an error Using 'body(): ResponseBody?' is an error. moved to val with okhttp

Using response.body() gives me an error of "Using 'body(): ResponseBody?' is an error. moved to val" i tried removing ? but nothing works the error is in body()

        override fun onResponse(call: Call, response: Response) {
            val body = response.body()?.string();

            println(body)
            println("Sucees")
like image 637
CHALA19X Avatar asked Jul 06 '19 10:07

CHALA19X


People also ask

How do you get a response body in OkHttp?

OkHttp Response To implement our JSON decoder, we need to extract the JSON from the result of the service call. For this, we can access the body via the body() method of the Response object.


1 Answers

It looks like you're using OkHttp 4.0.0.

The response.body() function has been deprecated. Instead, you need to access the body as a val, like this:

override fun onResponse(call: Call, response: Response) {
            val body = response.body?.string();

            println(body)
            println("Sucees")
}

Let me know if that helps!

like image 72
Segun Famisa Avatar answered Oct 02 '22 19:10

Segun Famisa