Loopj AsyncHttpClient JsonHttpResponseHandler 没有得到超时异常
Loopj AsyncHttpClient JsonHttpResponseHandler not getting a timeout exception
我正在尝试使用以下代码连接到服务器。但是,即使关闭服务器后我也无法超时。我想知道我哪里出错了。
但是我确实调用了 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 个错误发现了这一点,这些错误期望第二个功能得以实现。
我正在尝试使用以下代码连接到服务器。但是,即使关闭服务器后我也无法超时。我想知道我哪里出错了。
但是我确实调用了 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 个错误发现了这一点,这些错误期望第二个功能得以实现。