Created
July 2, 2017 12:53
-
-
Save the-dagger/5d894a2a0f6273fd9c701d0a29539ebe to your computer and use it in GitHub Desktop.
This file contains 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
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