Skip to content

Instantly share code, notes, and snippets.

@ziginsider
Created May 6, 2018 08:51
Show Gist options
  • Save ziginsider/ba18bfcd3fb46ebf122814b007390e8b to your computer and use it in GitHub Desktop.
Save ziginsider/ba18bfcd3fb46ebf122814b007390e8b to your computer and use it in GitHub Desktop.
private fun startWork(path: String) {
Thread {
val reader: BufferedReader? = null
val buf = StringBuilder()
try {
val url = URL(path)
val c = url.openConnection() as HttpURLConnection
c.setRequestMethod("GET")
c.setReadTimeout(10000)
c.connect()
if (c.responseCode != HttpURLConnection.HTTP_OK) {
runOnUiThread({
Toast.makeText(this, "responce isn't OK", Toast.LENGTH_SHORT).show()
})
}
val reader = BufferedReader(InputStreamReader(c.getInputStream()))
var line = reader.readLine()
while (line != null) {
buf.append(line + "\n")
line = reader.readLine()
}
} catch (e: Exception) {
e.printStackTrace()
}
finally {
reader?.close()
}
val subString = buf.toString().substring(buf.toString().indexOf("(") + 1,
buf.toString().indexOf(")"))
val jsonObject = JSONObject(subString)
val showMyIPobject = jsonObject.getString("ip")
runOnUiThread({
ipText.text = showMyIPobject
})
}.start()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment