使用截击处理 android 中的响应超时
Handling response timeout in android using volley
我试图在 android 中使用 volley 捕获基本上两种类型的错误。我已将 errorListener 用于失败的 api 调用。下面是我正在做的。
Response.ErrorListener errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if(error instanceof NoConnectionError)
{
// Did not connect
}
else if(error instanceof TimeoutError)
{
//Response Timeout error is what I have assumed.
}
}
};
我基本上有兴趣区分 ConnectionTimeOut 和 ResponseTimeOut。这是正确的吗
编辑:
我需要拨打 API 电话进行一些验证,我使用的是 2G。请求已发送到服务器,但由于超时未收到响应。所以我想知道请求是否已发送到服务器,或者请求根本没有发送到服务器。根据我的假设,如果客户端没有连接到服务器,它将是连接超时错误,否则如果请求被发送到服务器并且客户端没有获得响应,那么它将是超时。那么我的理解是正确的还是我遗漏了什么?
我想你可以为此使用 HTTP 代码。
如果是 408 错误,则是请求超时
The client did not produce a request within the time that the server was prepared to wait. The client MAY repeat the request without modifications at any later time.
如果是 504
The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server specified by the URI (e.g. HTTP, FTP, LDAP) or some other auxiliary server (e.g. DNS) it needed to access in attempting to complete the request.
要获取错误代码,您需要执行以下操作:
error.networkResponse.statusCode
我试图在 android 中使用 volley 捕获基本上两种类型的错误。我已将 errorListener 用于失败的 api 调用。下面是我正在做的。
Response.ErrorListener errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if(error instanceof NoConnectionError)
{
// Did not connect
}
else if(error instanceof TimeoutError)
{
//Response Timeout error is what I have assumed.
}
}
};
我基本上有兴趣区分 ConnectionTimeOut 和 ResponseTimeOut。这是正确的吗
编辑: 我需要拨打 API 电话进行一些验证,我使用的是 2G。请求已发送到服务器,但由于超时未收到响应。所以我想知道请求是否已发送到服务器,或者请求根本没有发送到服务器。根据我的假设,如果客户端没有连接到服务器,它将是连接超时错误,否则如果请求被发送到服务器并且客户端没有获得响应,那么它将是超时。那么我的理解是正确的还是我遗漏了什么?
我想你可以为此使用 HTTP 代码。
如果是 408 错误,则是请求超时
The client did not produce a request within the time that the server was prepared to wait. The client MAY repeat the request without modifications at any later time.
如果是 504
The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server specified by the URI (e.g. HTTP, FTP, LDAP) or some other auxiliary server (e.g. DNS) it needed to access in attempting to complete the request.
要获取错误代码,您需要执行以下操作:
error.networkResponse.statusCode