Created
April 24, 2024 10:14
-
-
Save tiagodavi70/a366715556f186e98bffdcbd511b7a72 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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