2017-09-16 69 views
0

我正在處理Volley POST請求,並在單獨的線程中運行該文章。我看到的問題是,即使設置了超時值,RequestFuture也會立即超時(小於一秒)。有人可以幫忙嗎?服務器URL是可訪問的,它返回200 OK,但RequestFuture get不會等待服務器響應。 錯誤:java.util.concurrent.ExecutionException:com.android.volley.TimeoutError排球網絡總是超時

Thread t = new Thread(new Runnable() { 
      @Override 
      public void run() { 
       RequestFuture<JSONObject> future = RequestFuture.newFuture(); 
       JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, jsonBody, future, future); 
       RequestQueue queue = Volley.newRequestQueue(context); 
       queue.add(request); 

       try { 
        JSONObject response = future.get(50, TimeUnit.SECONDS); 
        if(response != null) { 
         DialogUtility.alert(context, response.toString()); 
        } 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } catch (ExecutionException e) { 
        e.printStackTrace(); 
       } catch (TimeoutException e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 

     t.start(); 

回答

0

在你的代碼試試這個。

request.setRetryPolicy(new DefaultRetryPolicy(100000, 5, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 
  1. DefaultRetryPolicyRetryPolicy實現,並且RetryPolicy是一個接口;
  2. DefaultRetryPolicy構造函數中的參數1是設置的超時時間,默認值是2500.您可以將其設置得稍長;
  3. 構造函數DefaultRetryPolicy中的參數2用於設置重複請求的最大數目,默認值爲1,可以設置爲0;
  4. DefaultRetryPolicy構造函數中的參數3設置爲允許指定可用於實現<索引返回的退避乘數 爲避免從RESTful服務器請求數據,默認值爲1,當爲1時,這可以簡單地理解爲「每次請求在超時請求之前很久」long * 2>「等等。