WHOATEMYNOODLES
WHOATEMYNOODLES

Reputation: 2731

Why does Retrofit reponse.body().toString() not display the JSON from response?

I'm making a Retrofit call:

RetrofitInterfaces.IGetEvent service = RetrofitClientInstance.getRetrofitInstance().create(RetrofitInterfaces.IGetEvent.class);
        Call<ResponseBody> call = service.listRepos("1", "9e028aaa-d265-4e27-9528-30858ed8c13d");
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
                Log.d(TAG, "onResponse: URL: " + response.raw().request().url());
                if(response.isSuccessful() && response.body() != null){
                    Log.d(TAG, "onResponse: " + response.body().toString());
                }
            }

            @Override
            public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
                Log.d(TAG, "onFailure: Error: " + t);
            }
        });

However it returns it the log as: okhttp3.ResponseBody$1@fcf58e0

But when I do

try {
   System.out.println(response.body().string());
} catch (IOException e) {
   e.printStackTrace();
}

It returns me the JSON {"server_time":1580949319,"event_info":[{"e_id":1

Why does the first one not return the JSON String?

Upvotes: 0

Views: 627

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007124

Why does the first one not return the JSON String?

Because you are calling toString(), whereas in your second snippet, you are calling string(). string() returns the JSON. toString(), in this case, returns the default implementation defined on the Object class, which is a mangled combination of the class name and the object ID.

Upvotes: 3

Related Questions