2017-02-10 49 views
1

我想將我的應用程序超時時間設置爲60秒,這意味着我的應用程序只會在ProgressDialog從服務器獲取答覆或達到超時但未從服務器獲得答覆時關閉ProgressDialog。排球setRetryPolicy超時不起作用

目前我與排球庫工作在Android上,所以這是我做的:

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毫秒。 在此先感謝

回答

4

因爲它不只是無法連接到主機 - 它無法找到它的路由。這意味着它永遠不會與主持人通話。因此,它立即返回。重試策略正在工作,但僅適用於連接可能的情況。如果服務器拒絕連接或其他一些條件,它也會立即結束。

+0

感謝您的回答! – VincentTheonardo