Skip to content

Instantly share code, notes, and snippets.

@jaydeepw
Created August 10, 2015 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaydeepw/b66aeb3edcbbfc63dddd to your computer and use it in GitHub Desktop.
Save jaydeepw/b66aeb3edcbbfc63dddd to your computer and use it in GitHub Desktop.
Converting Retrofit Response to String.
@Nullable
/**
* Converts {@link retrofit.client.Response#body} to simple {@link java.lang.String} representation.
* This can be used to log message or manually parse JSON or any other purpose.
* @param response
* @return
*/
public static String toString(@NonNull Response response) {
if (response == null) {
return null;
}
TypedInput body = response.getBody();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(body.in()));
StringBuilder outString = new StringBuilder();
String newLine = System.getProperty("line.separator");
String line;
while ((line = reader.readLine()) != null) {
outString.append(line);
outString.append(newLine);
}
// Prints the correct String representation of body.
return outString.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment