Skip to content

Instantly share code, notes, and snippets.

@isaacpompa
Last active September 29, 2016 18:09
Show Gist options
  • Select an option

  • Save isaacpompa/d1dbba6f080ec24ce5aad91478143340 to your computer and use it in GitHub Desktop.

Select an option

Save isaacpompa/d1dbba6f080ec24ce5aad91478143340 to your computer and use it in GitHub Desktop.
View the API response body
private String getBodyResponse(HttpURLConnection connection){
String line = null;
BufferedReader br = null;
StringBuilder sb = null;
String body = null;
int response_code;
InputStreamReader isr;
try {
if (connection != null){
response_code = connection.getResponseCode();
isr = (response_code == 200 || response_code == 204) ? new InputStreamReader(connection.getInputStream()) :
new InputStreamReader(connection.getErrorStream());
br = new BufferedReader(isr);
sb = new StringBuilder();
while ( (line = br.readLine()) != null) {
sb.append(line);
}
body = sb != null ? sb.toString() : null;
} else {
System.out.println("getBodyResponse [ERROR]: No HttpURLConnection provided" );
body = null;
}
} catch (Exception ex) {
System.out.println("getBodyResponse [EXEPTION] " + ex);
}
return body;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment