Volley Android, recover server response

Asked

Viewed 185 times

0

Hello, I am using Volley for Android to connect to my Webservice, but I would like to display the server response independent of the Status that returns HTTP via webservice. explaining better:

When I call the webservice and the answer comes as 200 in the Status it enters the 'onResponse' of Volley and I can check what I returned, but when returning any other status it enters the 'onErrorResponse' and here inside I can not see which message return from the Webservice. I know by 'error' I can know the status and some errors already show the user, but I needed to display some information that only my return from Webservice has.

Here the connection code.

StringRequest stringRequisicao = new StringRequest(tipoRequisicao, url+funcao, new Response.Listener<String>() {

        @Override
        public void onResponse(String respostaWs) {

            try {

                JSONObject objeto = new JSONObject(respostaWs);
                retorno.onSuccess(objeto);

            } catch (JSONException e) {

                System.out.println("Catch : "+e.getMessage());

            }

        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            if (error instanceof TimeoutError || error instanceof NoConnectionError) {

                aviso = Toast.makeText(contexto, "[WSF01] Sem Conexão ou Timeout", Toast.LENGTH_LONG);
                aviso.show();

            } else if (error instanceof AuthFailureError) {

                aviso = Toast.makeText(contexto, "[WSF02] Falha na autenticação", Toast.LENGTH_LONG);
                aviso.show();

            } else if (error instanceof ServerError) {

                aviso = Toast.makeText(contexto, "[WSF03] Falha no Servidor", Toast.LENGTH_LONG);
                aviso.show();

            } else if (error instanceof NetworkError) {

                aviso = Toast.makeText(contexto, "[WSF04] Falha na Conexão", Toast.LENGTH_LONG);
                aviso.show();

            } else if (error instanceof ParseError) {

                aviso = Toast.makeText(contexto, "[WSF05] Falha Parse Error", Toast.LENGTH_LONG);
                aviso.show();

            } else {

                aviso = Toast.makeText(contexto, "[WSF06] Falha Não identificada", Toast.LENGTH_LONG);
                aviso.show();

            }
            String erro = "[WSF] ";
            if (error.getMessage() != null){
                erro += error.getMessage().toString().trim();
            }

            if (error.networkResponse != null) {
                erro += " Status: "+error.networkResponse.statusCode;
            }

            System.out.println(error.networkResponse);

            aviso = Toast.makeText(contexto,erro, Toast.LENGTH_LONG);
            aviso.show();

        }
    })
    {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<>();
            String auth = "Basic ";

            if (getCredenciais() != null){
                auth += Base64.encodeToString(getCredenciais().getBytes(), Base64.NO_WRAP);
            }

            headers.put("Accept", "application/json");
            headers.put("Authorization", auth);
            headers.put("User-Agent", "Megamil");
            return headers;
        }

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


    };

1 answer

0

Try to use the getMessage() in your Exception, it may contain the cause of the error. Example

 @Override
        public void onErrorResponse(VolleyError error) {
              Toast.makeText(contexto, error.getMessage(), Toast.LENGTH_LONG).show;
.
.
.

See if this solution helps you.

  • so I already do that, it presents the error treated by Volley, not the server response =/

Browser other questions tagged

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