我在网络慢的情况下使用 Volley 发送 POST 请求时遇到问题。每次我在 LogCat 中看到 BasicNetwork.logSlowRequests
时,我的 POST 请求会被执行两次或更多次,导致一个请求产生多个(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);
请帮忙,我拼命寻找解决这个问题的方法。
Volley
的源代码,我会发现大多数超时时间都设置为5000毫秒。我猜你的帖子是重复的,因为您的重试在请求超时之前发生,实际上发送了两次。 - tolgap