Skip to content

Instantly share code, notes, and snippets.

@dferreira-cvl
Created January 9, 2019 15:33
Show Gist options
  • Save dferreira-cvl/50697a479ebdb331b2e8a7ead2c8f4b5 to your computer and use it in GitHub Desktop.
Save dferreira-cvl/50697a479ebdb331b2e8a7ead2c8f4b5 to your computer and use it in GitHub Desktop.
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