the_prole the_prole - 1 month ago 24
Android Question

How to change Volley request rate

I'm making multiple requests to Amazon Web Services. I'm getting the 503 error because I'm making too many request too quickly. I want to know how to set the time-out between different requests, not the same ones. I am not looking to set the retry policy. I am also not looking to time-trigger individual requests. I want to time the interval between requests. The reason is that I am looping so quickly and making so many requests, that timing-triggering them is equivalent to submitting them all that the same time. The whole point is to space the requests out evenly.

BNK BNK
Answer

Since you don't show how you made multiple requests, so I suggest you refer to the following sample, then try applying to your code. Hope it helps!

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);            

        final RequestQueue queue = Volley.newRequestQueue(this);
        final String url = "http://google.com";
        final Handler handler = new Handler();
        for (int i = 0; i <= 5; i++) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    StringRequest request = new StringRequest(url, new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            Log.i("onResponse", response);                                
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.e("onErrorResponse", error.toString());
                        }
                    });    
                    queue.add(request);
                }
            }, 2000); // 2000 miliseconds
        }
    }