Automatically update data obtained from json api - Volley

Asked

Viewed 198 times

0

Hello guys I have the following problem I have an application that uses Volley to get certain data json through api up to there all right it works properly but the problem is Volley cache that does not reset until the application is closed completely, example as long as the cel is not shut down or the application itself is closed by the task manager the json data will not update in what way I can do so that in example every 30 minutes the obtained data is updated.

My Code:

JsonArrayRequest arrayreq = new JsonArrayRequest(JsonURL, new Response.Listener<JSONArray>() {

                    @Override
                    public void onResponse(JSONArray response) {
                        try {
                            JSONObject obj = response.getJSONObject(0);
                            String valor = obj.getString("valor");
                            String data = obj.getString("data");

                            results.setText("Vale R$: " + valor );
                            datav.setText("Última atualização: " + data);

                        }  catch (JSONException e) {
                            e.printStackTrace();
                        }}},
                new Response.ErrorListener() {
                    @Override
                    // Handles errors that occur due to Volley
                    public void onErrorResponse(VolleyError error) {
                        results.setText("Erro ao obter dados");
                        Log.e("Volley", "Error");
                    }
                }
        );
        requestQueue.add(arrayreq);

2 answers

0

You can use the Alarmmanager and set each time to perform the request again.

However the implementation of this Alarm to call a function you must create a service. It’s a bit complex to implement, but there is good content there in the Android API itself.

I had something similar but it was also executed when the App should be finished. I hope to have helped

Or else in the raw mode, you create a thread running every X-time within an infinite loop.

0

You can use Alarmmanager to run every x minutes as already said, or use the google framework Syncadapter, where you also arrow how much time will run the code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.