Loopj AsyncHttpClient JsonHttpResponseHandler 未获得超时异常



我正在尝试使用以下代码连接到服务器。但是,即使关闭服务器,我也无法超时。我想知道我哪里出错了。

但是,我确实会调用onFinish函数。但无法区分是因为成功完成请求还是其他原因。

我想知道为什么我没有超时,以及如何更改我的代码以实现一个函数来处理服务器关闭时的超时。

 fun getJSONData() {
        Toast.makeText(this@LoginActivity, " Starting to Get Data", Toast.LENGTH_LONG).show()
        val client = AsyncHttpClient()
        client.addHeader("X-Csrf-Token", "fetch")
        client.addHeader("Accept", "application/json")
        client.setBasicAuth("userid", "pwd")
        client.responseTimeout = 1000
        client.setTimeout(1000)
        client.connectTimeout = 1000
        Toast.makeText(this@LoginActivity, "---->" + client.connectTimeout + ':' + client.responseTimeout, Toast.LENGTH_LONG).show()
        client.get(getURL, object : JsonHttpResponseHandler() {
            override fun onSuccess(statusCode: Int, headers: Array<out Header>, response: JSONObject) {
                super.onSuccess(statusCode, headers, response)
              // do further processing
            }
            override fun onFailure(statusCode: Int, headers: Array<out Header>?, responseString: String?, throwable: Throwable?) {
                super.onFailure(statusCode, headers, responseString, throwable)
            }
            override fun onFinish() {
                super.onFinish()

            }
            override fun onRetry(retryNo: Int) {
                super.onRetry(retryNo)
            }
        })
    }

这个是为我工作的

 try {
                URL url = new URL(sUrl);
                HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
                connection.setConnectTimeout(iConnectTimeout); //default 9999ms
                connection.setReadTimeout(iReadTimeout);    // def 9999
                connection.setRequestMethod(sMethod);

我们实现了具有不同参数的onFailure的覆盖功能,并且像魅力一样工作。

替换此代码

override fun onFailure(statusCode: Int, headers: Array<out Header>?, responseString: String?, throwable: Throwable?) {
            super.onFailure(statusCode, headers, responseString, throwable)
        }

有了这个

override fun onFailure(statusCode: Int, headers: Array<out Header>?, responseString: String?, throwable: Throwable?) {
                super.onFailure(statusCode, headers, responseString, throwable)
            }

我们通过浏览此应用程序的 logcat 错误来发现这一点,该错误期望实现第二个函数。

相关内容

  • 没有找到相关文章

最新更新