Raju Haldar RH3X4R
Raju Haldar RH3X4R

Reputation: 13

how to make volley request in loop until specific response come

How to make Volley request in loop until specific response come?

package raju.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.skyfishjy.library.RippleBackgroun;
public class xxx extends AppCompatActivity {
    int xi= 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xxx);
       StringRequest sr = new StringRequest("http://reju.pe.hu/test.php", new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response.contains("1")){
                    xi=1;
                }
                Log.w("res",response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        while (xi==0){
            try {
                Thread.sleep(1000);
                RequestQueue xx = Volley.newRequestQueue(this);
                xx.add(sr);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

Upvotes: 0

Views: 1541

Answers (1)

Karun Shrestha
Karun Shrestha

Reputation: 733

Here I have created a method called runRequest, which has the code you've given above. The only changes I've made to the code is that I am waiting for the response to the request, checking if it's a specific response. If it's not the specific response, the method runRequest is called again, and if it's the specific response, nothing is done.

public void runRequest() {
    StringRequest sr = new StringRequest("http://reju.pe.hu/test.php", new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            if (response.contains("1")) {
                //here comes the specific response
            } else {
                runRequest();
                //re run the request
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    });
    RequestQueue xx = Volley.newRequestQueue(this);
    xx.add(sr);
}

Upvotes: 1

Related Questions