为什么HttpURLConnection停止执行?

问题描述 投票:0回答:1

我想使用此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时,任务不会继续执行,就好像执行永远冻结了一样。怎么了?

android kotlin android-asynctask httpurlconnection
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.