How to update / refresh the data using Android Volley with Mysql?

Asked

Viewed 29 times

0

I’m new on the subject and new here, I’m displaying an excerpt of the code I’m using for testing. The data is normally displayed the first time I query through the application, but if I perform a new update, update, or delete, the data is not updated in the application, it remains displaying the old data.

I have a more complete app that I am developing but I came across this error and crashed. I have tried to clear the cache and disable but without result.

public void CarregarWebServices()
{
    progresso = new ProgressDialog(this);
    progresso.setMessage("Carregando...");
    progresso.show();
    String url = "https://meu_endreco/arquivo.php";

    jsonObjectReq = new JsonObjectRequest(Request.Method.GET, url,null,this,this);
    request.add(jsonObjectReq);

}

@Override
public void onErrorResponse(VolleyError error)
{
    progresso.hide();//fechando
    Toast.makeText(this,"Erro: "+error,Toast.LENGTH_LONG).show();
}
@Override
public void onResponse(JSONObject response)
{
    progresso.hide();//fechando
    textView.setText(response.toString());//exibindo os dados
}

1 answer

0

managed to solve my problem by adding jsonArrayRequest.setShouldCache(false); and Queue.getCache(). clear();

I also added a GET/POST method and code even if the parameters are empty:

protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            return params;
        }

Follow an example:

public void BuscarJson(String url) {
    // Initialize a new JsonArrayRequest instance
    jsonArrayRequest = new JsonArrayRequest(
            Request.Method.POST,
            url,
            null,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    try{
                        for(int i=0;i<response.length();i++){
                            JSONObject obj = response.getJSONObject(i);
                            //guardando valor retornado na variável
                            String variavel = obj.getString("meu_campo_json");
                            //também poderá adicionar uma lista de valores (ArrayList)

                        }
                    }catch (JSONException e){
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener(){
                @Override
                public void onErrorResponse(VolleyError error){
                    Toast.makeText(getContext(),"Erro: "+error,Toast.LENGTH_LONG).show();
                }
            }
    ){
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            return params;
        }};

    jsonArrayRequest.setShouldCache(false);
    RequestQueue queue = Volley.newRequestQueue(getContext());
    queue.getCache().clear();
    queue.add(jsonArrayRequest);

}

If you didn’t want to, you can remove the repeat loop

Browser other questions tagged

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