Working Volley Android Error

Asked

Viewed 78 times

1

I’m creating an application for Android that sends via Volley some strings. I would like to know how I work errors, for example, if the server is offline.

Follow the code for sending strings:

private void salvarNaWeb(final User objUsuario) {

    StringRequest stringRequest = new StringRequest(Request.Method.POST,
            URL_SALVAR_USUARIOWEB,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }
    ) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {

            Map<String, String> params = new Hashtable<String, String>();


            params.put("nome", objUsuario.getNome());
            params.put("email", objUsuario.getEmail());
            params.put("telefone", objUsuario.getTelefone());
            params.put("senha", objUsuario.getSenha());
            params.put("latitude", latitude + "");
            params.put("longitude", longitude + "");

            mudarTela();
            return params;
        }
    };

    stringRequest.setRetryPolicy(new DefaultRetryPolicy(
            AppBoloNaHora.WEB_SERVICE_TIMEOUT_MS,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));


    RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
    requestQueue.add(stringRequest);
}

I would like to make a validation, to know if the data were actually sent. Because I’m only doing a check if the phone is with internet to validate the sending/ insertion of data in the database.

Thank you.

1 answer

0


Hello, you need to get Networkresponse to know what kind of error was caused.

new Response.ErrorListener() {
      @Override
      public void onErrorResponse(VolleyError error) {
          NetworkResponse error_code = error.networkResponse;

          if(error_code == null){
               // Sem conexão a internet no aparelho
               funcaoErroSemInternet();
          }else{
               // Sem conexão ao servidor
               funcaoServidorOffline();
          }
      }
}

Browser other questions tagged

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