Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a raw Retrofit response string?

I am working on Retrofit, but I am stuck on one thing: how do I get the raw JSON on the 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 the "MenuItems" log I am not getting the JSON response, it's coming something like this

MenuItems: Response :: retrofit2.Response@e292dd4

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

like image 548
Anshuman Pattnaik Avatar asked Aug 09 '16 21:08

Anshuman Pattnaik


People also ask

How do you handle retrofit response in HTML?

For this, we will use the ResponseBody Object provided by the Retrofit only. When a response is received in the callback, we will extract the body of the response and convert it to a string by calling the string() method. Now we will Create a document from this string by passing it to Jsoup. parse()method.


2 Answers

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

like image 62
Salem Avatar answered Sep 20 '22 20:09

Salem


Use Call<JSONObject>. That way you don't even need to include any of retrofit converters in your project.

like image 21
maciekjanusz Avatar answered Sep 18 '22 20:09

maciekjanusz