【问题标题】:Android volley response received more than onceAndroid volley 响应收到不止一次
【发布时间】:2017-05-27 17:27:11
【问题描述】:

我正在使用 Volley 从服务器获取数据。有时当网络连接速度很慢时,我会得到两次或三次或更多的响应。我在Android volley sending data twice 中遵循了类似问题的答案。但我仍然是遇到同样的问题。下面是我的代码。请帮助我

代码:

 public void volley_get_list() {
    RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
            StringRequest stringRequest = new StringRequest(Request.Method.POST, srch_lst_url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response)
                {
                    Log.e("Response", response);
                    if(!broadcast_unregister) {
                        getActivity().unregisterReceiver(broadcastReceiver);
                        Log.e("BroadcastReceiver", "Unregistered");

                        broadcast_unregister=true;
                    }
                        }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                error.printStackTrace();

                }
            }) {
                @Override
                protected Map<String, String> getParams() {
                    HashMap<String,String> hashMap = new HashMap<>();
                    hashMap.put("search", typed_text);
                    hashMap.put("latt", lat);
                    hashMap.put("long", longt);

                    return hashMap;
                }
            };
     RetryPolicy retryPolicy=new DefaultRetryPolicy(0,DefaultRetryPolicy.DEFAULT_MAX_RETRIES,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
            stringRequest.setRetryPolicy(retryPolicy);
            stringRequest.setShouldCache(false);
            requestQueue.add(stringRequest);
            requestQueue.start();
       }        

【问题讨论】:

    标签: android android-volley


    【解决方案1】:

    我希望,如果您删除此代码,它应该可以工作 (requestQueue.start();),因为添加到请求队列本身会使 api 命中。

    希望这有帮助:)

    【讨论】:

      【解决方案2】:

      您将重试超时时间设置为 0 秒,这意味着它将在第一次重试之后非常非常快地开始重试尝试,如果您这样做时它们是错误,我并不感到惊讶。

      https://android.googlesource.com/platform/frameworks/volley/+/idea133/src/com/android/volley/DefaultRetryPolicy.java#58

      int timeout = TimeUnit.SECONDS.toMillis(3);
      int maxRetries = DefaultRetryPolicy.DEFAULT_MAX_RETRIES;
      int backoffMultiplier = DefaultRetryPolicy.DEFAULT_BACKOFF_MULT;
      RetryPolicy retryPolicy = new DefaultRetryPolicy(timeout, maxRetries, backoffMultiplier);
      

      尝试 3 秒(3000 毫秒)

      【讨论】:

        猜你喜欢
        • 2018-12-12
        • 1970-01-01
        • 2015-11-18
        • 2015-03-18
        • 1970-01-01
        • 2017-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多