Can't get OkHttp's response.body.toString() to return a string
You have use .string()
function to print the response in System.out.println()
. But at last in Log.i()
you are using .toString()
.
So please use .string()
on response body to print and get your request's response, like:
response.body().string();
NOTE:
.toString()
: This returns your object in string format..string()
: This returns your response.
I think this solve your problem... Right.
Just in case someone bumps into the same weird thing as I have. I run my code during development in Debug Mode and apparently since OKHttp 2.4
..the response body is a one-shot value that may be consumed only once
So when in debug there is a call "behind the scene" from the inspector and the body is always empty. See: https://square.github.io/okhttp/3.x/okhttp/okhttp3/ResponseBody.html
The response.body,.string()
can be consumed only once.
Please use as below:
String responseBodyString = response.body.string();
use the responseBodyString as needed in your application.