如何在 Volley 中设置连接超时?
How to set a conectivity timeout in Volley?
我正在使用 Volley 来使用 Rest 服务,并且我已经使用 setRetryPolicy
方法
实现了 1500 毫秒的超时
// test [
final long ini = System.currentTimeMillis();
RequestQueue queue = Volley.newRequestQueue(appActivity);
final ApiRequest request = new ApiRequest(url, parameters, ignoreNull,
new Consumer<String>() {
@Override
public void accept(String response) {
Log.d("test","ok");
}
},
new Consumer<Exception>() {
@Override
public void accept(Exception exception) {
exception.printStackTrace();
Log.d("test","time: " + (System.currentTimeMillis()-ini) );
}
});
RetryPolicy policy = new DefaultRetryPolicy(1500, 1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
request.setRetryPolicy(policy);
queue.add(request);
// test]
当我测试应用程序并启动服务时,应用程序运行良好,当服务缓慢时,应用程序按预期在 4500 毫秒(1500 + 3000)后显示超时异常,但是当我停止 de服务器,应用程序在超过 4500 毫秒后显示不同的异常
14 秒后出现 ConnectException
com.android.volley.NoConnectionError: java.net.ConnectException: Failed to connect to api.roomnette.com/172.67.155.71:443
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:181)
at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:131)
at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:111)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:90)
Caused by: java.net.ConnectException: Failed to connect to api.roomnette.com/172.67.155.71:443
at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:143)
at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:112)
at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:184)
at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:258)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(Unknown Source:0)
at com.android.volley.toolbox.HurlStack.addBody(HurlStack.java:292)
at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:277)
at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:249)
at com.android.volley.toolbox.HurlStack.executeRequest(HurlStack.java:94)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:123)
... 3 more
2020-06-25 22:03:41.854 5132-5132/com.roomnette D/test: time: 14784
23 秒后出现 UnknownHostException
com.android.volley.NoConnectionError: java.net.UnknownHostException: Unable to resolve host "api.roomnette.com": No address associated with hostname
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:181)
at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:131)
at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:111)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:90)
Caused by: java.net.UnknownHostException: Unable to resolve host "api.roomnette.com": No address associated with hostname
at java.net.InetAddress.lookupHostByName(InetAddress.java:440)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.android.okhttp.internal.Network.resolveInetAddresses(Network.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:358)
at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:341)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:331)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:249)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:437)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:245)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
at com.android.volley.toolbox.HurlStack.addBody(HurlStack.java:292)
at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:277)
at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:249)
at com.android.volley.toolbox.HurlStack.executeRequest(HurlStack.java:94)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:123)
... 3 more
2020-06-25 22:14:25.365 5132-5132/com.roomnette D/test: time: 23842
很明显,当我停止服务器时它会导致 UnknownHostException/ConnectException 但是,为什么应用程序需要超过 10 秒才能抛出异常?有没有办法设置超时以尝试解析主机或建立连接?
响应是预期的。当您停止服务器时会导致未知主机异常
我遇到了同样的问题,通过添加延迟任务停止队列解决了:
RetryPolicy policy = new DefaultRetryPolicy(1500, 1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
request.setRetryPolicy(policy);
// solution [
int maxTimeOut = 15000;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
queue.stop();
}
}, maxTimeOut);
// solution]
queue.add(stringRequest);
我正在使用 Volley 来使用 Rest 服务,并且我已经使用 setRetryPolicy
方法
// test [
final long ini = System.currentTimeMillis();
RequestQueue queue = Volley.newRequestQueue(appActivity);
final ApiRequest request = new ApiRequest(url, parameters, ignoreNull,
new Consumer<String>() {
@Override
public void accept(String response) {
Log.d("test","ok");
}
},
new Consumer<Exception>() {
@Override
public void accept(Exception exception) {
exception.printStackTrace();
Log.d("test","time: " + (System.currentTimeMillis()-ini) );
}
});
RetryPolicy policy = new DefaultRetryPolicy(1500, 1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
request.setRetryPolicy(policy);
queue.add(request);
// test]
当我测试应用程序并启动服务时,应用程序运行良好,当服务缓慢时,应用程序按预期在 4500 毫秒(1500 + 3000)后显示超时异常,但是当我停止 de服务器,应用程序在超过 4500 毫秒后显示不同的异常
14 秒后出现 ConnectException
com.android.volley.NoConnectionError: java.net.ConnectException: Failed to connect to api.roomnette.com/172.67.155.71:443
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:181)
at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:131)
at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:111)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:90)
Caused by: java.net.ConnectException: Failed to connect to api.roomnette.com/172.67.155.71:443
at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:143)
at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:112)
at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:184)
at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:258)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(Unknown Source:0)
at com.android.volley.toolbox.HurlStack.addBody(HurlStack.java:292)
at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:277)
at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:249)
at com.android.volley.toolbox.HurlStack.executeRequest(HurlStack.java:94)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:123)
... 3 more
2020-06-25 22:03:41.854 5132-5132/com.roomnette D/test: time: 14784
23 秒后出现 UnknownHostException
com.android.volley.NoConnectionError: java.net.UnknownHostException: Unable to resolve host "api.roomnette.com": No address associated with hostname
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:181)
at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:131)
at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:111)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:90)
Caused by: java.net.UnknownHostException: Unable to resolve host "api.roomnette.com": No address associated with hostname
at java.net.InetAddress.lookupHostByName(InetAddress.java:440)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.android.okhttp.internal.Network.resolveInetAddresses(Network.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:358)
at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:341)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:331)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:249)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:437)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:245)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
at com.android.volley.toolbox.HurlStack.addBody(HurlStack.java:292)
at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:277)
at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:249)
at com.android.volley.toolbox.HurlStack.executeRequest(HurlStack.java:94)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:123)
... 3 more
2020-06-25 22:14:25.365 5132-5132/com.roomnette D/test: time: 23842
很明显,当我停止服务器时它会导致 UnknownHostException/ConnectException 但是,为什么应用程序需要超过 10 秒才能抛出异常?有没有办法设置超时以尝试解析主机或建立连接?
响应是预期的。当您停止服务器时会导致未知主机异常
我遇到了同样的问题,通过添加延迟任务停止队列解决了:
RetryPolicy policy = new DefaultRetryPolicy(1500, 1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
request.setRetryPolicy(policy);
// solution [
int maxTimeOut = 15000;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
queue.stop();
}
}, maxTimeOut);
// solution]
queue.add(stringRequest);