Skip to content

Instantly share code, notes, and snippets.

@luctrudeau
Created June 23, 2015 20:09
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 luctrudeau/3cd09671672c82622e78 to your computer and use it in GitHub Desktop.
Save luctrudeau/3cd09671672c82622e78 to your computer and use it in GitHub Desktop.
HTTPURLConnection
AsyncTask<URL, String, String> httpTask = new AsyncTask<URL, String, String>() {
@Override
protected String doInBackground(URL... urls) {
HttpURLConnection urlConnection = null;
StringBuilder sb = new StringBuilder();
try {
urlConnection = (HttpURLConnection) urls[0].openConnection();
try (BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()))) {
for (String line = in.readLine(); line != null; line = in.readLine()) {
sb.append(line);
}
} finally {
urlConnection.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
@Override
protected void onPostExecute(String s) {
Log.d("httpTASK", s);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment