Skip to content

Instantly share code, notes, and snippets.

@MediumOne
Created September 23, 2015 08:15
Show Gist options
  • Save MediumOne/72362fe627363531d767 to your computer and use it in GitHub Desktop.
Save MediumOne/72362fe627363531d767 to your computer and use it in GitHub Desktop.
Snippet for HTTPUrlConnection in Android
new Thread() {
@Override
public void run() {
HttpURLConnection urlConnection = null;
try {
URL url = new URL("http://google.com");
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
//Apache commons IOUtils
String result = IOUtils.toString(in);
Logger.e(TAG, "result - " + result);
} catch (MalformedURLException e) {
Logger.e(TAG, "error mfe : " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
Logger.e(TAG, "error io : " + e.getMessage());
e.printStackTrace();
} finally {
if(urlConnection != null){
urlConnection.disconnect();
}
}
}
}.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment