I am translating some app from TIZEN language to WEAR OS Google. Some of that send a POST JSON to server. In TIZEN with simply JS . . I am able to send data with WATCH connected to mobile device by Bluetooth connected to internet (mobile device), no app specific installed into mobile. This is the code x TIZEN
function btnclicked(cmd) {
switch (cmd) {
case 'AA':
json = '{"pop":"clock1"}';
break;
case 'BB':
json = '{"pop":"clock2"}';
break;
}
var client = new XMLHttpRequest();
client.open("POST", "https://xxxxxxx.it:8132/yyy/qqqqq");
client.setRequestHeader("Content-Type", "application/json");
client.onreadystatechange = function() {
if (client.readyState == 4 && client.status == 200) {
alert(client.status);
}
}
client.send(json);
}
Now i try to "translate" into KOTLING with equivalent function
fun pushToChat(cmdx: String) {
val serverURL: String = "https://xxxxxxx.it:8132/yyy/qqqqq"
val url = URL(serverURL)
val connection = url.openConnection() as HttpURLConnection
connection.requestMethod = "POST"
connection.connectTimeout = 300000
connection.doOutput = true
val message = "{\"cmd\":\"$cmdx\"}"
val postData: ByteArray = message.toByteArray(StandardCharsets.UTF_8)
connection.setRequestProperty("charset", "utf-8")
connection.setRequestProperty("Content-length", postData.size.toString())
connection.setRequestProperty("Content-Type", "application/json")
try {
val outputStream: DataOutputStream = DataOutputStream(connection.outputStream)
outputStream.write(postData)
outputStream.flush()
} catch (exception: Exception) {
Log.d("cmd1","Eccezione")
}
if (connection.responseCode != HttpURLConnection.HTTP_OK && connection.responseCode != HttpURLConnection.HTTP_CREATED) {
try {
val inputStream: DataInputStream = DataInputStream(connection.inputStream)
val reader: BufferedReader = BufferedReader(InputStreamReader(inputStream))
val output: String = reader.readLine()
println("There was error while connecting")
System.exit(0)
} catch (exception: Exception) {
throw Exception("Exception while push the notification $exception.message")
}
}
}
testing with emulator rise an exception on Log.d("cmd1","Eccezione")
Have to use other library like okHTTP or Retrofit? I found some message (very old) that the issue could be because watch is not connetcted directly to net with Wi-Fi, but I need to use mobile device connection with internet (without dedicated app running on mobile device).
With this "unlock" i resolve the problem, now i need a fine tuning of the code, and verify on watch (with emulator it is ok)
val policy = ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)