freddieptf freddieptf - 4 months ago 20
Android Question

Can't get OkHttp's response.body.toString() to return a string

I'm trying to get some json data using OkHttp and can't figure out why when i try logging the

what i get is
Results:﹕ com.squareup.okhttp.Call$RealResponseBody@41c16aa8

try {
URL url = new URL(BaseUrl);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()

Call call = client.newCall(request);
Response response = call.execute();

**//for some reason this successfully prints out the response**
System.out.println("YEAH: " + response.body().string());

if(!response.isSuccessful()) {
Log.i("Response code", " " + response.code());

Log.i("Response code", response.code() + " ");
String results = response.body().toString();

Log.i("OkHTTP Results: ", results);


I don't know what i'm doing wrong here. How do i get the response 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() to print and get your request's response.


  1. .toString(): This returns your object in string format.

  2. .string(): This returns your response.

I think this solve your problem... Right.