Created
January 9, 2019 15:33
-
-
Save dferreira-cvl/50697a479ebdb331b2e8a7ead2c8f4b5 to your computer and use it in GitHub Desktop.
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
private static class HTTPReqTask extends AsyncTask<Void, Void, Void> { | |
@Override | |
protected Void doInBackground(Void... params) { | |
HttpURLConnection urlConnection = null; | |
try { | |
URL url = new URL("https://reqres.in/api/users?page=2"); | |
urlConnection = (HttpURLConnection) url.openConnection(); | |
int code = urlConnection.getResponseCode(); | |
if (code != 200) { | |
throw new IOException("Invalid response from server: " + code); | |
} | |
BufferedReader rd = new BufferedReader(new InputStreamReader( | |
urlConnection.getInputStream())); | |
String line; | |
while ((line = rd.readLine()) != null) { | |
Log.i("data", line); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
if (urlConnection != null) { | |
urlConnection.disconnect(); | |
} | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment