alan samuel
alan samuel

Reputation: 425

Volley request only executing once in android Java

I am making a volley request like this. I want to do a post command maybe once or more than one time depending on the size of my arraylist. But doing this code only executes the volley command once.

Here is what I tried.

for(int i = 0; i<listofobjects.size(); i++){
    makeRequest(listobjects.get(i));
}


public void makeRequest(Customobject obj){

 RequestQueue requestQueue = Volley.newRequestQueue(this);
    String modifiedUrl = MainActivity.URL + "XXXXX";
    StringRequest stringRequest = new StringRequest(Request.Method.POST, modifiedUrl, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.v("Response", response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.v("Error", error.toString());
        }
})};

requestQueue.add(stringRequest);

}

This code only executes the volley request once. How can I make it execute depending on the number of items i have in listofobjects.

Upvotes: 0

Views: 1211

Answers (3)

Naimatullah
Naimatullah

Reputation: 4079

// Gloabal variable
int i = 0;

Call this function

makeRequest(listobjects.get(i));

makeRequest method will be called recursively

public void makeRequest(Customobject obj){

 RequestQueue requestQueue = Volley.newRequestQueue(this);
    String modifiedUrl = MainActivity.URL + "XXXXX";
    StringRequest stringRequest = new StringRequest(Request.Method.POST, modifiedUrl, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.v("Response", response);
            i++;
            if(i < listobjects.size()){
                // make a recursive function 
                makeRequest(listobjects.get(i));
            } else {
                // Completed
           }

            

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.v("Error", error.toString());
        }
})};

requestQueue.add(stringRequest);

}

Upvotes: 1

UltimateDevil
UltimateDevil

Reputation: 2847

I think this method will solve your problem.

int i = 0;
makeRequest(listObjects.get(i));
public void makeRequest(Customobject obj){
 RequestQueue requestQueue = Volley.newRequestQueue(this);
    String modifiedUrl = MainActivity.URL + "XXXXX";
    StringRequest stringRequest = new StringRequest(Request.Method.POST, modifiedUrl, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.v("Response", response);
                 if(reposnse==success && i<listobjects.size){
                  i++;
                  makeRequest(listObjects.get(i));
             }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.v("Error", error.toString());
        }
})};

requestQueue.add(stringRequest);

}

Upvotes: 1

Vicky Kumar
Vicky Kumar

Reputation: 154

Try this:

    public void makeRequest(Customobject obj){
    boolean status = true;
    int i=0;
    do
    {
     RequestQueue requestQueue = Volley.newRequestQueue(this);
        String modifiedUrl = MainActivity.URL + "XXXXX";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, modifiedUrl, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                status = true;
                Log.v("Response", response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                status = true;
                Log.v("Error", error.toString());
            }
    })};
    if(status)
{
    requestQueue.add(stringRequest);
    i++;
        status =false;        

}
    }while(i<listofobjects.size());

    }

Upvotes: 1

Related Questions