This file contains hidden or 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
data class Student( | |
@SerializedName("key_name") | |
var name: String? = null, | |
@SerializedName("key_address") | |
var address: String? = null) { | |
} | |
val student = Student("Mark", "London, 12000") // object | |
val json = Gson().toJson(student) // json string |
This file contains hidden or 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
val json1 = """ | |
{ | |
"name": "Mark", | |
"address": "London": | |
}""" | |
val student = Gson().fromJson(json1, Student::class.java) |
This file contains hidden or 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
val student = Student("Alex", "Rome, 1500") // instance | |
val jsonString = Gson().toJson(student) // json string |
This file contains hidden or 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
data class Student ( | |
var name: String? = null, | |
var address: String? = null) { | |
} | |
val student = Student("Alex", "Rome") // instance |
This file contains hidden or 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
dependencies { | |
... | |
implementation 'com.google.code.gson:gson:2.8.5' | |
... | |
} |
This file contains hidden or 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
private fun loadWebView(){ | |
val loader = Thread { | |
when { | |
MyReachability.hasServerConnected(this) -> runOnUiThread { | |
hideLoadingDialog() | |
Log.d(classTag, "Loading ${Constants.LANDING_SERVER}") | |
mWebView.loadUrl(Constants.LANDING_SERVER) | |
} | |
MyReachability.hasInternetConnected(this) -> runOnUiThread { |
This file contains hidden or 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
object MyReachability { | |
private fun hasNetworkAvailable(context: Context): Boolean { | |
val service = Context.CONNECTIVITY_SERVICE | |
val manager = context.getSystemService(service) as ConnectivityManager? | |
val network = manager?.activeNetworkInfo | |
Log.d(classTag, "hasNetworkAvailable: ${(network != null)}") | |
return (network?.isConnected) ?: false | |
} |
This file contains hidden or 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)}") |
This file contains hidden or 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.REACHABILITY_SERVER = "https://www.google.com" | |
fun hasInternetConnected(context: Context): Boolean { | |
if (hasNetworkAvailable(context)) { | |
try { | |
val connection = URL(Constants.REACHABILITY_SERVER).openConnection() as HttpURLConnection | |
connection.setRequestProperty("User-Agent", "ConnectionTest") | |
connection.setRequestProperty("Connection", "close") | |
connection.connectTimeout = 1000 // configurable | |
connection.connect() | |
Logger.d(classTag, "hasInternetConnected: ${(connection.responseCode == 200)}") |
This file contains hidden or 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
private fun hasNetworkAvailable(context: Context): Boolean { | |
val service = Context.CONNECTIVITY_SERVICE | |
val manager = context.getSystemService(service) as ConnectivityManager? | |
val network = manager?.activeNetworkInfo | |
Logger.d(classTag, "hasNetworkAvailable: ${(network != null)}") | |
return (network != null) | |
} |