当我的数据库运行时,一切都很好。但是当我没有运行时,我的移动应用程序总会崩溃
错误讯息:
引起:java.net.ConnectException:无法连接到/ httpURL。
如何解决问题?
这是我的代码:
LISTAGG
答案 0 :(得分:2)
由于代码中没有catch
块,因此您当前没有捕获任何异常。
如果您想处理ConnectException
,那么您只需抓住它:
override fun doInBackground(vararg url: String?): String {
var text = ""
var connection: HttpURLConnection? = null
try {
connection = URL(url[0]).openConnection() as HttpURLConnection
connection.connect()
text = connection.inputStream.use {
it.reader().use { reader ->
reader.readText()
}
}
} catch (ce: ConnectException) {
// ConnectionException occurred, do whatever you'd like
ce.printStackTrace()
} catch (e: Exception) {
// some other Exception occurred
e.printStackTrace()
} finally {
connection?.disconnect()
}
return text
}