Skip to content

Instantly share code, notes, and snippets.

@madnotdead
Last active September 21, 2019 13:49
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 madnotdead/d9eeb86f1c38696dbb5741f7b32bf451 to your computer and use it in GitHub Desktop.
Save madnotdead/d9eeb86f1c38696dbb5741f7b32bf451 to your computer and use it in GitHub Desktop.
Networking utility to get data from a URL
private val SLEEP_TIME = 5000L
private val TAG = "CounterAsyncTask"
private fun sendGet(urlToCall: String = "https://www.google.com/") {
Thread.sleep(SLEEP_TIME)
val url = URL(urlToCall)
with(url.openConnection() as HttpURLConnection) {
requestMethod = "GET" // optional default is GET
Log.i(TAG,"\nSent 'GET' request to URL : $url; Response Code : $responseCode")
inputStream.bufferedReader().use {
it.forEachLine { Log.i(TAG, it) }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment