我想使用此AsyncTask ping url
@SuppressLint("StaticFieldLeak")
private inner class ActivationAsyncTask : AsyncTask<Void, Void, Int>() {
override fun doInBackground(vararg params: Void?): Int? {
HttpURLConnection.setFollowRedirects(false)
val activationUrl = ActivationManager.shared.getActivationUrl()
val httpURLConnection = (URL(activationUrl).openConnection() as HttpURLConnection)
httpURLConnection.requestMethod = "HEAD"
var result = 520
result = try {
httpURLConnection.responseCode // trouble is here
} catch (e: IOException) {
520
} catch (e: UnknownHostException) {
520
}
Log.d("Tag", "This log never shows up")
return result
}
override fun onPostExecute(result: Int?) {
super.onPostExecute(result)
val response = when (result) {
200 -> MainHttpStatus.OK
401 -> MainHttpStatus.Unauthorized
else -> MainHttpStatus.Unknown
}
listener.completion(response)
}
}
但是当我尝试获取.responseCode
时,任务不会继续执行,就好像执行永远冻结了一样。怎么了?