Skip to content

Instantly share code, notes, and snippets.

@hissain
Created May 5, 2020 18:21
Show Gist options
  • Save hissain/1367186bb45b8a584db092b30f7fff7f to your computer and use it in GitHub Desktop.
Save hissain/1367186bb45b8a584db092b30f7fff7f to your computer and use it in GitHub Desktop.
//Constants.LANDING_SERVER = "https://www.myserver.com"
fun hasServerConnected(context: Context): Boolean {
if (hasNetworkAvailable(context)) {
try {
val connection = URL(Constants.LANDING_SERVER).openConnection() as HttpURLConnection
connection.setRequestProperty("User-Agent", "Test")
connection.setRequestProperty("Connection", "close")
connection.connectTimeout = 1000 //configurable
connection.connect()
Logger.d(classTag, "hasServerConnected: ${(connection.responseCode == 200)}")
return (connection.responseCode == 200)
} catch (e: IOException) {
Logger.e(classTag, "Error checking server connection", e)
}
} else {
Logger.w(classTag, "Server is unavailable!")
}
Logger.d(classTag, "hasServerConnected: false")
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment