Automatically update data obtained from api json - Volley

0

Hello everyone, I have an application that uses a volley to get a given json data, so it works fine but the problem is the volley cache that does not reset until the application is completely closed, for example while the cell is not shut down or the application itself is closed by the task manager json data will not update in what way I can do so that in example every 30 minutes the 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);
    
asked by anonymous 23.01.2017 / 20:59

2 answers

0

You can use the AlarmManager library.  and set each X time to make the request again.

However, implementing this Alarm to call a function should create a service . It's a bit complex to implement, but there's good content there in the Android API itself.

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

Or else in the roughest mode, you create a thread running each time X within an infinite loop.

    
25.01.2017 / 02:33
0

You can use AlarmManager to run every x minutes as stated, or use google framework SyncAdapter , where you also arrow how much in how long will you run the code.

    
26.01.2017 / 20:30