Created
May 6, 2018 08:51
-
-
Save ziginsider/ba18bfcd3fb46ebf122814b007390e8b to your computer and use it in GitHub Desktop.
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 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