Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit callback get response body

I am testing Retrofit to compare it with Volley and I am struggling to get the response from my requests. For example, I do something like this:

RestAdapter restAdapter = new RestAdapter.Builder()             .setEndpoint("http://localhost:8080")             .build();  MyService service = restAdapter.create(MyService.class); service.getToto("toto", new Callback<Toto>() {          @Override         public void success(Toto toto, Response response) {             // Try to get response body             BufferedReader reader = null;             StringBuilder sb = new StringBuilder();             try {                 reader = new BufferedReader(                     new InputStreamReader(response.getBody().in()));                 String line;                 try {                     while ((line = reader.readLine()) != null) {                         sb.append(line);                     }                 } catch (IOException e) {                     e.printStackTrace();                 }             } catch (IOException e) {                 e.printStackTrace();             }             String result = sb.toString();         }          @Override         public void failure(RetrofitError error) {}     }); 

It works, the object toto is set, but for testing purposes, I also want to display the JSON response returned by the server.

So I am trying to read the InputStream from response.getBody() which is a TypedInputStream. Unfortunately, I always get an IOException : Stream is closed.

I tried to use the Utils class from Retrofit but I get the same IOException error.

like image 238
Kerwan Avatar asked Mar 11 '14 12:03

Kerwan


People also ask

How do I return a retrofit response?

Actually you cant return a value , instead you can try making a method for passing/storing data in DTO & call that method in your respose. IsSuccessful . Pass the object of response json to the method for storing your required stuffs like token, username n etc,.

How do I get message from error body in retrofit?

you can simply use "response. message(). toString()" which will give the same error string in a more readable format.

What is retrofit callback?

Retrofit uses two different callback methods for the two possible outcomes of a network requests: either a failure or a successful request. Retrofit will call the appropriate callback method depending on the result. If the request was successful, Retrofit will also pass you the response of the server.


1 Answers

Inside callback's angle brackets write "Response" and then extract the stream from this response.

service.getToto("toto", new Callback<Response>() {     @Override     public void success(Response result, Response response) {          //Try to get response body         BufferedReader reader = null;         StringBuilder sb = new StringBuilder();         try {              reader = new BufferedReader(new InputStreamReader(result.getBody().in()));              String line;              try {                 while ((line = reader.readLine()) != null) {                     sb.append(line);                 }             } catch (IOException e) {                 e.printStackTrace();             }         } catch (IOException e) {             e.printStackTrace();         }           String result = sb.toString();     }      @Override     public void failure(RetrofitError error) {      } }); 
like image 105
Nitesh Kumar Avatar answered Sep 19 '22 13:09

Nitesh Kumar