0

I am using Volley to acquire data from server.Some times when the network connection is slow I get the response twice or thrice or more than that.I followed the answer from a similar problem in Android volley sending data twice .But still I am stuck with the same problem.Below is my code.Please help me

Code:

 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();
       }        
jobin
  • 1,489
  • 5
  • 27
  • 52

2 Answers2

0

I hope, if you remove this code it should work ( requestQueue.start();), because the add to the requestqueue itself make the api hit.

Hope this is helpful :)

Jeevanandhan
  • 1,073
  • 10
  • 18
0

You are setting the retry timeout period to 0 seconds, this means it'll start the retry attempt very very very soon after the first one, and I am not surprised if their are bugs when you do this.

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

try 3 seconds instead (3000ms)

Blundell
  • 75,855
  • 30
  • 208
  • 233