Bineesh Kumar
Bineesh Kumar

Reputation: 166

Android Volley duplicates request

Volley request is done inside a while. It duplicates for no reason ( seemingly ) For example when I suppose to make two requests, volley does it 4 times. Below is my code, some of you could hint the problem in my code ?

while(i<chnumTxt.length()){
            final RequestQueue queue;
            queue = Volley.newRequestQueue(this);
            char letter=chnumTxt.charAt(i);
            Log.i("check","counter="+i+" "+"digit="+letter);
            String URL = "http://192.168.4.20:80/chnumber?key="+letter;
            Log.i("web",URL);

            StringRequest request = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    //Log.i("html",response.toString());
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });
            
            queue.add(request);
            
            new CountDownTimer(500, 500) {
                public void onFinish() {
                    // When timer is finished
                    // Execute your code here
                }

                public void onTick(long millisUntilFinished) {
                    
                }
            }.start();
            
            i++;
        }

Upvotes: 1

Views: 592

Answers (1)

Bineesh Kumar
Bineesh Kumar

Reputation: 166

Eventually i found the answer somewhere. Volley will retry links if the link seems slow. So after blocking it, got it right. It is like :

First declare a variable :

static final float DEFAULT_BACKOFF_MULT = 1f;

and after defining the request, do below code :

request.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 0,
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

Upvotes: 5

Related Questions