2014-01-22 196 views
35
RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext()); 
mRequestQueue.add(new JsonObjectRequest(Method.GET, cityListUrl, null, new Listener<JSONObject>() 
{ 
    public void onResponse(JSONObject jsonResults) 
    { 
     //Any Call 
    } 
}, new ErrorListener() 
    { 
     public void onErrorResponse(VolleyError arg0) 
     { 
      //Any Error log 
     } 
    } 
)); 

這是我的請求調用,我想更改或設置請求超時。反正有可能嗎?排隊請求隊列超時

回答

112

應設置要求的RetryPolicy

myRequest.setRetryPolicy(new DefaultRetryPolicy(
       MY_SOCKET_TIMEOUT_MS, 
       DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
       DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 

這將更改您的代碼:

RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext()); 
JsonObjectRequest request = new JsonObjectRequest(Method.GET, cityListUrl, null, new 
    Listener<JSONObject>() { 
     public void onResponse(JSONObject jsonResults) { 
      //Any Call 
     } 
    }, new ErrorListener(){ 
     public void onErrorResponse(VolleyError arg0) { 
      //Any Error log 
     } 
    } 
); 


int socketTimeout = 30000;//30 seconds - change to what you want 
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT); 
request.setRetryPolicy(policy); 
mRequestQueue.add(request); 

如果只是剛開始接觸凌空,你可能想而不是考慮droidQuery,這有點容易配置:

int socketTimeout = 30000; 
$.ajax(new AjaxOptions().url(cityListUrl) 
         .timeout(socketTimeout) 
         .success(new Function() { 
          public void invoke($ d, Object... args) { 
           JSONObject jsonResults = (JSONObject) args[0]; 
           //Any call 
          } 
         }) 
         .error(new Function() { 
          public void invoke($ d, Object... args) { 
           AjaxError error = (AjaxError) args[0]; 
           Log.e("Ajax", error.toString()); 
          } 
         })); 
+0

有任何onTimeOut方法? –

+1

@ Mr.Hyde我不知道* Volley *中的一個,但是您可以通過使用* statusCode()*來處理* droidQuery *中的* statusCode()*,它接受可返回的狀態代碼數組,並且* Function *所以你可以傳入超時代碼(480,419,504,503,522,598,599)和一個*函數*來處理這些超時。 * varargs *的第一個參數將是一個* AjaxOptions *對象,您可以選擇性地重新啓動您的請求:'$ .ajax(((AjaxOptions)args [0]);' – Phil

+0

我試過相同的...一些它的工作時間....有時它會給timeoutError並很快重試....並再次給出相同的錯誤,... 如何識別和找到實際的原因? ... – Bhuro

7

像這樣的事情

RetryPolicy retryPolicy = new DefaultRetryPolicy(
    YOUR_TIMEOUT_MS, 
    YOUT_MAX_RETRIES, 
    YOUR_BACKOFF_MULT 
); 

JsonObjectRequest request = new JsonObjectRequest(...); 
request.setRetryPolicy(retryPolicy); 

或者你也可以實現自己的RetryPolicy

4

這是爲我工作:

RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext()); 
JsonObjectRequest request = new JsonObjectRequest(Method.GET, cityListUrl, null, new 
    Listener<JSONObject>() { 
     public void onResponse(JSONObject jsonResults) { 
      //Any Call 
     } 
    }, new ErrorListener(){ 
     public void onErrorResponse(VolleyError arg0) { 
      //Any Error log 
     } 
    } 
); 


int socketTimeout = 30000;//30 seconds - change to what you want 
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT); 
request.setRetryPolicy(policy); 
mRequestQueue.add(request); 
+0

android studio是一個有點兒車。但感謝解決我的問題一分鐘或一小時..要等待新的^ _ ^ –

1
void RequestVolley() { 

    // Instantiate the RequestQuee 
    RequestQueue queue = Volley.newRequestQueue(getApplication()); 

    //create new volley request 
    JsonObjectRequest requestNew = new JsonObjectRequest(Request.Method.GET, Url, null, createMyReqSuccessListener(), createMyReqErrorListener()); 

    //Response.Listener and Error.Listener defined afterwards 


    //first param is TIMEOUT ...integer 
    //second param is number of retries ...integer 
    //third is backoff multiplier ...integer 

    requestNew.setRetryPolicy(new DefaultRetryPolicy(6000, 1, 1)); 

    queue.add(requestNew); 
} 

private Response.Listener <JSONObject> createMyReqSuccessListener() { 
    return new Response.Listener <JSONObject>() { 
     @Override 
     public void onResponse(JSONObject response) { 

      //do something 
     } 
    }; 
} 

private Response.ErrorListener createMyReqErrorListener() { 
    return new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 

      //do something 
     } 
    }; 
} 
1
String url = "https://api.joind.in/v2.1/events?start=" + start + "&resultsperpage=20&format=json"; 
Log.i("DREG", "onLoadMoreItems: " + url); 
final StringRequest stringRequest = new StringRequest(Request.Method.GET, url, 
     new Response.Listener<String>() { 
      @Override 
      public void onResponse(String response) { 
       // Add Code Here 
      } 
     }, 
     new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
        if (error instanceof NetworkError) { 
        } else if (error instanceof ServerError) { 
        } else if (error instanceof AuthFailureError) { 
        } else if (error instanceof ParseError) { 
        } else if (error instanceof NoConnectionError) { 
        } else if (error instanceof TimeoutError) { 
         Toast.makeText(getContext(), 
           "Oops. Timeout error!", 
           Toast.LENGTH_LONG).show(); 
        } 
      } 
     } 
); 
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
     10000, 
     DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
     DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 
requestQueue.add(stringRequest);