При подключении к сайту java.io.FileNotFoundException в android
Подключаюсь к сайту https://time.is/Unix_time_now для того чтобы узнать время при выполнении hrCon.getInputStream() стабильный вылет с исключением java.io.FileNotFoundException
fun test() {
CoroutineScope(Dispatchers.IO).launch {
try {
var nowTime: Long = getTime()
} catch (e: Exception) {
L.i("exception:" + e.toString())
}
}
}
@Throws(Exception::class)
private fun getTime(): Long {
val url = "https://time.is/Unix_time_now"
var hrCon: HttpsURLConnection = URL(url).openConnection() as HttpsURLConnection
if (hrCon.getResponseCode() != HttpURLConnection.HTTP_OK) {
var istream = hrCon.getInputStream()
val doc: Document = Jsoup.parse(istream, "UTF-8", url)
val tags = arrayOf(
"div[id=time_section]",
"div[id=clock0_bg]"
)
var elements: Elements = doc.select(tags[0])
for (i in tags.indices) {
elements = elements.select(tags[i])
}
return elements.text().toLong()
} else {
return -1
}
}