【问题标题】:Loopj AsyncHttpClient JsonHttpResponseHandler not getting a timeout exceptionLoopj AsyncHttpClient JsonHttpResponseHandler 没有收到超时异常
【发布时间】:2019-01-19 13:38:58
【问题描述】:

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

但是我确实接到了 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)
            }
        })
    }

【问题讨论】:

    标签: android json kotlin timeout loopj


    【解决方案1】:

    这个是为我工作的

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

    【讨论】:

    • 我找到了解决方案。给大家分享一下
    【解决方案2】:

    我们使用不同的参数实现了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 错误发现了这一点,该应用程序期望实现第二个功能。

    【讨论】:

    • 有什么区别?
    猜你喜欢
    • 2014-01-18
    • 2014-05-30
    • 1970-01-01
    • 2016-08-24
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    相关资源
    最近更新 更多