凌空设置重试策略超时不起作用



我想将我的应用程序超时持续时间设置为 60 秒,这意味着我的应用程序只会在收到服务器回复或达到超时而没有收到服务器的回复时关闭 ProgressDialog。

目前我正在安卓上使用 Volley 库,所以我就是这样做的:

private void loginOnline(final String user, final String pwd, final String login_url){
        final ProgressDialog pd = new ProgressDialog(this);
        pd.setMessage("Communicating with Server");
        pd.show();
        final RequestQueue queue = Volley.newRequestQueue(this);
        Map<String, String> params = new HashMap<String, String>();
        params.put(KEY_USERNAME, user);
        params.put(KEY_PASSWORD, pwd);
        final JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST, login_url, new JSONObject(params),
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        pd.dismiss();
                        try {
                            int msg = response.getInt("status");
                            sendMessage(msg);
                        }
                        catch (JSONException e){
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                pd.dismiss();
                Log.d("D", "onErrorResponse: "+error.getMessage());
            }
        });
        jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(60000,0,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(jsonObjReq);
    }

问题是当我尝试连接到我的服务器时,它显示在日志中:

D/D: onErrorResponse: java.net.ConnectException: failed to connect to /192.123.x.xxx (port 3000) after 60000ms: isConnected failed: EHOSTUNREACH (No route to host)
D/Volley: [1] Request.finish: 3072 ms: [ ] http://192.123.4.215:3000/login 0xdde27c7c NORMAL 1

我的问题是为什么它在达到 60000 毫秒之前停止连接到服务器。提前致谢

因为它不仅无法连接到主机 - 它无法找到通往主机的路由。 这意味着它永远无法与主机通信。 因此,它会立即返回。 重试策略有效,但仅当连接可能时才适用。 如果服务器拒绝连接或其他一些情况,它也会立即结束。

最新更新