Created
August 10, 2015 12:23
-
-
Save jaydeepw/b66aeb3edcbbfc63dddd to your computer and use it in GitHub Desktop.
Converting Retrofit Response to String.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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