Skip to content
Advertisement

Repeat Volley Request Every x-seconds Java

Alright – Before you say this is a duplicate, I’ve looked over every stack overflow article I can find, and none of them work and/or answer the question properly/simply. All I need is to repeat a function with a volley request inside of it every x-seconds.

Basically, I have a fairly simple Volley request inside a function, which works absolutely perfectly on one call.

Volley function:

  private void SetBusPositions() {
        TextView textE = (TextView) findViewById(R.id.FirstInfo);
        RequestQueue Queue = ServerRequestsQueue.getInstance(context.getApplicationContext()).getRequestQueue();
        int SendMethod = Request.Method.GET;
        String ServerURL = "my url";
        JsonArrayRequest JOR = new JsonArrayRequest(SendMethod, ServerURL, null, new Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray resp) {
                textE.setText(resp.toString());
                System.out.println("Response is: " + resp.toString());
                //for each object in JSON Array
                for (int i = 0; i < resp.length(); i++) {
                }
            }
        }, new Response.ErrorListener() {
            public void onErrorResponse(VolleyError error) {
             //process
            }
        });
        Queue.add(JOR);
    }

I just want to call this function periodically, to receive data from the server and update my bus-position data. There has to be a fairly simple way to do this? I know I must be missing something, but none of the other answers seem to help.

Also, as I’m using Google maps, my class is already extending FragmentActivity. I’ve seen methods that extend Runnable to get this working — but my Java is a bit rusty here. I’ve been doing too much JS.

Advertisement

Answer

private final class RepeatedOperation extends AsyncTask<Map, Void, String> {

    @Override
    protected String doInBackground(Map... params) {
    
        SetBusPosition(params[0]);

    }

    @Override
    protected void onPostExecute(String result) {
    }
}


private void callAsynchronousTask() {
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {       
    @Override
    public void run() {
        handler.post(new Runnable() {
            public void run() {       
                try {
                    RepeatedOperation performBackgroundTask = new RepeatedOperation();
                    // RepeatedOperation this class is the class that extends AsyncTask 
                    performBackgroundTask.execute(map);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                }
            }
        });
    }
};
timer.schedule(doAsynchronousTask, 0, 50000); //execute in every 50000 ms
}

Try this out and add this in the same scope as SetBusPosition()

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement