Android 如何禁用 Volley 请求重试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23146945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable Volley request from retrying?
提问by xiaowoo
I post a JsonRequest to a server. The server is slow. Volley tends to make multiple calls to the slow server because it didn't get a response from the first request (since my server is slow). Is there any way to disable Volley from retrying a request so that it can receive the first response?
我将 JsonRequest 发布到服务器。服务器很慢。Volley 倾向于多次调用慢速服务器,因为它没有从第一个请求中得到响应(因为我的服务器很慢)。有什么方法可以禁止 Volley 重试请求,以便它可以接收第一个响应?
I have tried:
我试过了:
myRequest.setRetryPolicy(new DefaultRetryPolicy(
TIMEOUT_MS,
RETRIES,
BACKOFF_MULT));
I have replaced TIMEOUT_MS = 0, RETRIES = 0, and even BACKOFF_MULT = 0, but it doesn't work.
我已经替换了 TIMEOUT_MS = 0、RETRIES = 0,甚至 BACKOFF_MULT = 0,但它不起作用。
Any other suggestions?
还有其他建议吗?
回答by David
The Volley Default Retry Policy is:
Volley 默认重试策略是:
/** The default socket timeout in milliseconds */
public static final int DEFAULT_TIMEOUT_MS = 2500;
/** The default number of retries */
public static final int DEFAULT_MAX_RETRIES = 1;
/** The default backoff multiplier */
public static final float DEFAULT_BACKOFF_MULT = 1f;
You can find it in DefaultRetryPolicy.java,
您可以在 DefaultRetryPolicy.java 中找到它,
so you can see that volley makes 1 retry request by default.
所以你可以看到 volley 默认发出 1 次重试请求。
Try to use smaller TIMEOUT (if you don't want to wait the 2500ms), or bigger than 2500ms to get the answer), but keep the other values, for example:
尝试使用较小的 TIMEOUT(如果您不想等待 2500ms)或大于 2500ms 以获得答案),但保留其他值,例如:
// Wait 20 seconds and don't retry more than once
myRequest.setRetryPolicy(new DefaultRetryPolicy(
(int) TimeUnit.SECONDS.toMillis(20),
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Hence, to disable Volley from retrying you will need to do:
因此,要禁用 Volley 重试,您需要执行以下操作:
myRequest.setRetryPolicy(new DefaultRetryPolicy(
(int) TimeUnit.SECONDS.toMillis(20), //After the set time elapses the request will timeout
0,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
回答by Dwivedi Ji
Try this,
尝试这个,
// remove caching
jsObjRequest.setShouldCache(false);
// Wait 30 seconds and don't retry more than once
jsObjRequest.setRetryPolicy(new DefaultRetryPolicy(0, 0,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Method call form task
方法调用表单任务
public void callWebService(JSONObject jsonRequest) {
JsonObjectRequest jsObjRequest = new JsonObjectRequest(
Request.Method.POST, url + pageurl, jsonRequest,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
sendResponse(jsonObject);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
callBack.onError(error);
try {
if (progressDialog != null) {
progressDialog.dismiss();
progressDialog = null;
}
} catch (Exception e) {
Helper.customPrintStackTrace(e);
}
}
}
) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put(WebConfig.WebParams.APIKEY, Helper
.getStringValuefromPrefs(context,
SharedPreferencesKey.PREF_APIKEY));
params.put(WebConfig.WebParams.APITOKEN, Helper
.getStringValuefromPrefs(context,
SharedPreferencesKey.PREF_APITOKEN));
if (!Helper.getStringValuefromPrefs(context,
SharedPreferencesKey.PREF_USERID).equals("")) {
params.put(WebConfig.WebParams.USER_ID, Helper
.getStringValuefromPrefs(context,
SharedPreferencesKey.PREF_USERID));
} else {
params.put(WebConfig.WebParams.USER_ID, "0");
}
return params;
}
};
// remove caching
jsObjRequest.setShouldCache(false);
// Wait 30 seconds and don't retry more than once
jsObjRequest.setRetryPolicy(new DefaultRetryPolicy(0, 0,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// Access the RequestQueue through your singleton class.
VolleySingleton.getInstance(context).addToRequestQueue(jsObjRequest);
if (showDefultProcess) {
progressDialog.show();
}
}
回答by Pang
To stop Volleyfrom retrying a request, simply set the retry policy of the request to a DefaultRetryPolicy
with maxNumRetries
being 0:
要停止凌空从重试的要求,只需将请求到的重试策略DefaultRetryPolicy
与maxNumRetries
为0:
myRequest.setRetryPolicy(new DefaultRetryPolicy(
DefaultRetryPolicy.DEFAULT_TIMEOUT_MS,
0, // maxNumRetries = 0 means no retry
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
回答by mehmoodnisar125
Using your StringRequestObject or JsonObjectRequestObject or JsonArrayRequestObject call this method.
使用您的StringRequest对象或JsonObjectRequest对象或JsonArrayRequest对象调用此方法。
For example if the object is an instance of StringRequest, here is the method:
例如,如果对象是 StringRequest 的实例,则方法如下:
stringRequest.setRetryPolicy(new DefaultRetryPolicy(initialTimeoutMs, maxNumRetries,
backoffMultiplier ));
initialTimeoutMsThe initial timeout for the policy.
initialTimeoutMs 策略的初始超时。
maxNumRetriesThe maximum number of retries.
maxNumRetries最大重试次数。
backoffMultiplierBackoff multiplier for the policy.
backoffMultiplier策略的退避乘数。
Below are the parameters which I gave.
下面是我给出的参数。
stringRequest.setRetryPolicy(new DefaultRetryPolicy(10 * 1000, 0,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
First parameter says set InitialTimeout to 10 seconds.
第一个参数表示将 InitialTimeout 设置为 10 秒。
Second parameter says set retry count tries to 0.
第二个参数表示设置重试计数尝试为 0。
Third parameter is default.
第三个参数是默认值。