Anshuman Pattnaik Anshuman Pattnaik - 4 months ago 50
JSON Question

How to get raw Retrofit response string?

I am working on Retrofit, but i am little stuck in one thing, how do i get raw JSON on response body.

public interface ViewMenuItems {
@GET
Call<ResponseBody> listRepos(@Url String url);
}

ViewMenuItems viewMenuItems = ApiClient.getClient().create(ViewMenuItems.class);
Call<ResponseBody> responseBodyCall = viewMenuItems.listRepos(Webservices.MERCHANT + merchantId + Webservices.MENU_ITEMS_LASTMODIFIED);

responseBodyCall.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.d("MenuItems", "Response :: " + response.body().toString());
}

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


But in "MenuItems" log i am not getting JSON response, it's coming something like this

MenuItems: Response :: retrofit2.Response@e292dd4


Please kindly go through my post and suggest me some solution.

Answer

Try to use body().string() instead of body().toString()