gpt4 book ai didi

请求缓慢时的Android Volley双发

转载 作者:IT老高 更新时间:2023-10-28 13:25:14 27 4
gpt4 key购买 nike

我在慢速网络上遇到 Volley POST 请求问题。每次我在 LogCat 中看到 BasicNetwork.logSlowRequests 时,我的 POST 请求都会执行两次或更多次,从而导致 1 个请求的多个(2 个或更多)张贴。我已经将重试策略设置为 0,但它没有帮助。

这是我的 LogCat

03-16 01:31:35.674: D/Volley(5984): [19807] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://[myserver]/api/places 0xfa7d0c33 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/places 0xfa7d0c33 NORMAL 1

这是我的代码

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);

请帮忙,我正在拼命寻找这个问题的解决方案。

最佳答案

将以下值添加到您的请求对象:

request.setRetryPolicy(new DefaultRetryPolicy(
DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

这里:request 是您的 JsonObjectRequest 对象。根据 Volley 中 DefaultRetryPolicy 类中的 DEFAULT TIMEOUT VALUE 更改 multiplicator 的值。

您也可以将第一个参数设置为 0,如下所示:

request.setRetryPolicy(new DefaultRetryPolicy(
0,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

关于请求缓慢时的Android Volley双发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22428343/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com