Created
May 5, 2020 18:21
-
-
Save hissain/1367186bb45b8a584db092b30f7fff7f 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
//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