Skip to content

Instantly share code, notes, and snippets.

@the-dagger
Created July 2, 2017 12:53
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 the-dagger/5d894a2a0f6273fd9c701d0a29539ebe to your computer and use it in GitHub Desktop.
Save the-dagger/5d894a2a0f6273fd9c701d0a29539ebe to your computer and use it in GitHub Desktop.
try {
URL url = new URL("https://jsonplaceholder.typicode.com/posts");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setReadTimeout(3000);
httpURLConnection.setConnectTimeout(5000);
httpURLConnection.connect();
if (httpURLConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new IOException("HTTP error code" + httpURLConnection.getResponseCode());
}
InputStream inputStream = httpURLConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
result = stringBuilder.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment