请求缓慢时,Android Volley 双发布
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22428343/
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
Android Volley double post when have slow request
提问by aristo_sh
I have a problem with Volley POST request on slow network. Everytime I see BasicNetwork.logSlowRequests
in my LogCat, my POST request is executed twice or more resulting multiple (2 or more) postings for 1 request. I already set the retry policy to 0, but It doesn't help.
我在慢速网络上遇到 Volley POST 请求的问题。每次我BasicNetwork.logSlowRequests
在 LogCat 中看到时,我的 POST 请求都会执行两次或更多次,从而导致 1 个请求的多个(2 个或更多)张贴。我已经将重试策略设置为 0,但它没有帮助。
This is my LogCat
这是我的 LogCat
03-16 01:31:35.674: D/Volley(5984): [19807] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://[myserver]/api/places0xfa7d0c33 NORMAL 1> [lifetime=3824], [size=313], [rc=200], [retryCount=0] 03-16 01:31:35.704: D/Volley(5984): [1] Request.finish: 3853 ms: [ ] http://[myserver]/api/places0xfa7d0c33 NORMAL 1
03-16 01:31:35.674:D/Volley(5984):[19807] BasicNetwork.logSlowRequests:请求的 HTTP 响应=<[] http://[myserver]/api/places0xfa7d0c33 NORMAL 1> [lifetime=3824 ], [size=313], [rc=200], [retryCount=0] 03-16 01:31:35.704: D/Volley(5984): [1] Request.finish: 3853 ms: [ ] http:// /[myserver]/api/places0xfa7d0c33 正常 1
This is my code
这是我的代码
JSONObject body = new JSONObject();
try {
body.put(PROTO_BODY_AUTHORIZATION, Sessions.getActiveSession().getToken());
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.POST,
context.getResources().getString(R.string.server_address) + "/places",
body,
callback,
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, error.getMessage(), Toast.LENGTH_LONG).show();
}
}
);
request.setRetryPolicy(
new DefaultRetryPolicy(
DefaultRetryPolicy.DEFAULT_TIMEOUT_MS,
0,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
getRequestQueue().add(request);
Please help, I desperately finding solution for this problem.
请帮忙,我拼命寻找这个问题的解决方案。
采纳答案by Droid_Mechanic
Add the following values to your Request object:
将以下值添加到您的请求对象:
request.setRetryPolicy(new DefaultRetryPolicy(
DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Here:request is your object of JsonObjectRequest. Change the value of multiplicator according to the DEFAULT TIMEOUT VALUE in DefaultRetryPolicy class in Volley.
这里:request 是您的 JsonObjectRequest 对象。根据 Volley 中 DefaultRetryPolicy 类中的 DEFAULT TIMEOUT VALUE 更改乘法器的值。
You can also set the first argument to 0, like below:
您还可以将第一个参数设置为 0,如下所示:
request.setRetryPolicy(new DefaultRetryPolicy(
0,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
回答by Droid_Mechanic
Just setting the Timeout in the RetryPolicy to 0 is too little. After checking the source, you have to actually set the maximum number of retries < 0, as it is checking current <= max...
只是将 RetryPolicy 中的 Timeout 设置为 0 太少了。检查源后,您必须实际设置最大重试次数 < 0,因为它正在检查 current <= max...
I fixed double posting with setting the policy to the following
我通过将政策设置为以下内容来修复双重发布
new DefaultRetryPolicy(0, -1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Hope it helps!
希望能帮助到你!
回答by aristo_sh
I found the solution for the double post, I just set the timeout to 0.
我找到了双重帖子的解决方案,我只是将超时设置为0。
回答by xiaoyee
I found the solution for the multi post bug.
我找到了多帖子错误的解决方案。
Change the RetryPolicy. I set the timeout value to 50000ms, worked fine Like this:
更改重试策略。我将超时值设置为 50000 毫秒,工作正常像这样:
request.setRetryPolicy(
new DefaultRetryPolicy(
500000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
)
);
回答by Alberto Penas
You must set the RetryPolicy to 0 retries and ensure that the timeout is bigger than the server timeout.
您必须将 RetryPolicy 设置为 0 重试,并确保超时大于服务器超时。
setRetryPolicy(new DefaultRetryPolicy("bigger than server timeout",
0,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
回答by Arun Badole
I am able to solve this issue by two ways.
我可以通过两种方式解决这个问题。
First is changed the RetryPolicy
.
Simply set the timeout value to double of the default timeout. Worked fine. You can also try other values.
首先是更改了RetryPolicy
. 只需将超时值设置为默认超时的两倍。工作得很好。您也可以尝试其他值。
request.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Another way is by setting connection.setChunkedStreamingMode(0);
in openConnection
method HurlStack
class.
另一种方法是connection.setChunkedStreamingMode(0);
在openConnection
方法HurlStack
类中设置。
I am creating my RequestQueue
like this requestQueue = Volley.newRequestQueue(context, new HurlStack());
我正在创造我RequestQueue
这样的requestQueue = Volley.newRequestQueue(context, new HurlStack());
Hope it helps :)
希望能帮助到你 :)
回答by user921509
I asked a similar question here:
我在这里问了一个类似的问题:
Android Volley makes 2 requests to the server when retry policy is set to 0
当重试策略设置为 0 时,Android Volley 向服务器发出 2 个请求
I managed to solve this issue by setting the keep-alive property to false inside Android, e.g.:
我设法通过在 Android 中将 keep-alive 属性设置为 false 来解决这个问题,例如:
System.setProperty("http.keepAlive", "false")
I added this line of code inside the class where I import requestqueue and make the requests.
我在导入 requestqueue 并发出请求的类中添加了这行代码。
Also, check if you server has the keep-alive header.
另外,检查您的服务器是否具有保持活动的标头。
This posthelped get to the solution.
这篇文章有助于找到解决方案。
回答by Jatin Devani
please increase the setRetryPolicy time.
请增加 setRetryPolicy 时间。
request.setRetryPolicy(new DefaultRetryPolicy(
30000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Volley.newRequestQueue(this).add(equest);
回答by Vitali Pekelis
This works for me.
这对我有用。
public class MyRetryPolicyWithoutRetry implements RetryPolicy
{
@override
public int getCurrentTimeout()
{
return CONNECTION_TIME_OUT; /200000/
}
@Override
public int getCurrentRetryCount()
{
return 0;
}
@Override
public void retry(VolleyError error) throws VolleyError
{
throw(error);
}
}
To use:
使用:
request.setRetryPolicy(new MyRetryPolicyWithoutRetry());
回答by Amoliski
The only way I got the double requests to stop was to set the retry policy retries to -1
我停止双重请求的唯一方法是将重试策略重试设置为 -1
request.setRetryPolicy(new DefaultRetryPolicy(0, -1, 0));
I think this is because the DefaultRetryPolicy's logic for attempts remaining returns true if retryCount is 0 and Max retries is also 0 in the hasAttemptRemaining() method:
我认为这是因为如果在 hasAttemptRemaining() 方法中 retryCount 为 0 并且 Max retries 也为 0,则 DefaultRetryPolicy 的剩余尝试逻辑返回 true:
protected boolean hasAttemptRemaining() {
return this.mCurrentRetryCount <= this.mMaxNumRetries;
}