Skip to content

Instantly share code, notes, and snippets.

@tiagodavi70
Created April 24, 2024 10:14
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 tiagodavi70/a366715556f186e98bffdcbd511b7a72 to your computer and use it in GitHub Desktop.
Save tiagodavi70/a366715556f186e98bffdcbd511b7a72 to your computer and use it in GitHub Desktop.
fun fetchDataFromUrl(url: String): String {
val uri = URI.create(url)
val connection = uri.toURL().openConnection() as HttpURLConnection
connection.requestMethod = "GET"
val responseCode = connection.responseCode
return if (responseCode == HttpURLConnection.HTTP_OK) {
connection.inputStream.bufferedReader().use {
it.readText()
}
} else {
"Error: $responseCode"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment