How to extract and convert RapidAPI ResponseBody to JSON string in Android?

I am using a dictionary API via RapidAPI in my Android application, but I’m having difficulty retrieving the actual response data. When I log the response body, all I see is something like okhttp3.internal.http.RealResponseBody@7a2b8d1 instead of the expected JSON content.

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
    .url("https://mashape-community-urban-dictionary.p.rapidapi.com/define?term=check")
    .get()
    .addHeader("x-rapidapi-host", "mashape-community-urban-dictionary.p.rapidapi.com")
    .addHeader("x-rapidapi-key", API_KEY)
    .build();

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

Log.d("TAG", "response details: " + response.toString());
Log.d("TAG", "response headers: " + response.headers());
Log.d("TAG", "response content: " + response.body());
Log.d("TAG", "message: " + response.message());

The logs display:

Response details: Response{protocol=http/1.1, code=200, message=OK, url=https://mashape-community-urban-dictionary.p.rapidapi.com/define?term=check}

Response headers: Cache-Control: no-cache, no-store, must-revalidate
Content-Type: application/json; charset=utf-8
Date: Mon, 13 Apr 2020 11:03:35 GMT
Server: RapidAPI-1.1.0

Response content: okhttp3.internal.http.RealResponseBody@6e33b4c

Message: OK

How can I correctly get the JSON data out from this ResponseBody and convert it into a string?

yeah, that object is just a ref to the response. use response.body().string() to get the JSON. just a tip - you can only call string() once, so save it in a var if you need it multiple times or you’ll run into some errors!

You’re logging the ResponseBody object instead of its content. When you call response.body(), you get a ResponseBody instance - logging that just shows the object reference.

Fix it like this:

String responseData = response.body().string();
Log.d("TAG", "JSON response: " + responseData);

I’ve hit this same issue with OkHttp APIs. The ResponseBody.string() method pulls the entire response into memory as a string - perfect for JSON parsing. Works great for normal-sized responses, but you’ll want to stream larger ones.

Once you’ve got the string, parse it with Gson or JSONObject.

This happens all the time with OkHttp responses. That ResponseBody object you’re seeing? It’s just a reference to the response data, not the actual content. You need to call the string() method on the response body to get the JSON.

Replace your last log statement with:

String jsonResponse = response.body().string();
Log.d("TAG", "JSON content: " + jsonResponse);

Here’s the catch - you can only call string() once on a ResponseBody. After that, the response stream is consumed and you’ll get empty results or an exception if you try again. Need to use the response data multiple times? Store it in a String variable first like I showed above, then work with that variable instead of hitting the response body repeatedly.