添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

如何在Volley中设置连接性超时?

1 人关注

我使用Volley来消费一个Rest Service,我使用 setRetryPolicy 方法实现了1500ms的超时。

// 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)后显示超时异常,这是预期的,但当我停止服务器时,应用程序在超过4500毫秒后显示不同的异常。

14秒后出现一个连接异常

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秒后出现一个未知主机异常

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$1.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

很明显,当我停止服务器时,它会导致未知主机异常/连接异常,但是,为什么应用程序需要超过10秒的时间来抛出这个异常?是否有办法设置一个超时来尝试解决主机或建立连接?

java
android
android-volley
imperezivan
imperezivan
发布于 2020-06-26
2 个回答
Javier B.
Javier B.
发布于 2020-06-26
已采纳
0 人赞同

我也有同样的问题,通过添加一个延迟任务来停止队列,解决了这个问题。

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]