我尝试为请求设置超时。我知道我设定了请求政策。
stringRequest.setRetryPolicy(new DefaultRetryPolicy(10,
3,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(stringRequest);
我的要求是邮递员需要500毫秒。高级代码设置超时,请求触发错误。但它并没有重试。
This answer表示定义自己的政策。请求不会终止。
stringRequest.setRetryPolicy(new RetryPolicy() {
@Override
public int getCurrentTimeout() {
// Here goes the new timeout
return 10;
}
@Override
public int getCurrentRetryCount() {
// The max number of attempts
return 3;
}
@Override
public void retry(VolleyError error) throws VolleyError {
}
});
编辑我想获得超时错误并再次尝试给定的重试次数。
retry
方法连续触发少量数据(例如100 ms,200 ms)
答案 0 :(得分:0)
你可以像
一样增加时间return 3*60*1000;
这是三分钟内的时间。我使用了像我这样工作的波纹管
public VoleyErrorHandling(StringRequest stringRequest){
stringRequest.setRetryPolicy(new RetryPolicy() {
@Override
public int getCurrentTimeout() {
// Here goes the new timeout
return 5*60*1000;
}
@Override
public int getCurrentRetryCount() {
// The max number of attempts
return 5;
}
@Override
public void retry(VolleyError error) throws VolleyError {
// Here you could check if the retry count has gotten
// To the max number, and if so, send a VolleyError msg
// or something
//Log.d("err", error.toString());
}
});
}
之前调用它
//Begin set Time Out
new VoleyErrorHandling(stringRequest);
//End set Time out
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
答案 1 :(得分:0)
If your request taking 500ms time on postman so you can set timeout time in your code like this.
stringRequest.setRetryPolicy(new DefaultRetryPolicy(600,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Above 600 ---> is in milisecond
if you set 6000 ---> will be 6 second
if you set 60000 ---> will be 60 seconds or 1 minute.
So set according to your choice..