Android Volley, retrieve response from server

0

Hello, I'm using the Volley for Android to connect to my Webservice, however I'd like to display the status-independent server response that returns HTTP through the webservice. explaining better:

When I call the webservice and the response comes as 200 in the Status it goes into the 'onResponse' of the Volley and I can verify what I returned, however when I return any other status it goes into 'onErrorResponse' and here I can not see which message that Webservice returns. I know that by 'error' I 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 is 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();
        }


    };
    
asked by anonymous 27.12.2017 / 23:35

1 answer

0

Try to use getMessage() in your exception, it can 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.

    
28.12.2017 / 12:18