2

I work with KSOAP2 but i wish usign Volley and i can't connect a web service with Authentication. I have tried overwriting the method getHeader but in the log shows the following.

E/Volley: [593] BasicNetwork.performRequest: Unexpected response code 500 for http://myservice/register.asmx

this is my code:

   public void peticion() {
    final RequestQueue queue = Volley.newRequestQueue(this);

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

                Log.e("response", response);

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("VolleyError", error.getMessage());

        }
    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("UsuarioName", "user123");
            headers.put("UsuarioPass", "123456789");
            return headers;
        }
    };

    queue.add(stringRequest);

}

EDIT Fragment the my WebService

<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/MLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">  <soap12:Header>    <Autenticacion xmlns="http://tempuri.org/">      <suarioPass>string</UsuarioPass>      <UsuarioName>string</UsuarioName>    </Autenticacion>  </soap12:Header>

with ksoap work but with volley no.

-The data are correct

-The service work

I dont understand why not work with volley

Thank you for your help

David Hackro
  • 3,652
  • 6
  • 41
  • 61

2 Answers2

1

I think you can refer to the following code (I have tested with ASP.Net Web API), then use its logic for your app:

        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i(LOG_TAG, response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(LOG_TAG, error.toString());
            }
        }) {
            @Override
            public byte[] getBody() throws AuthFailureError {
                String paramsEncoding = "UTF-8";
                Map<String, String> params = new HashMap<>();
                params.put("grant_type", "password");
                params.put("username", "user");
                params.put("password", "pass");
                StringBuilder encodedParams = new StringBuilder();
                try {
                    for (Map.Entry<String, String> entry : params.entrySet()) {
                        encodedParams.append(URLEncoder.encode(entry.getKey(), paramsEncoding));
                        encodedParams.append('=');
                        encodedParams.append(URLEncoder.encode(entry.getValue(), paramsEncoding));
                        encodedParams.append('&');
                    }
                    return encodedParams.toString().getBytes(paramsEncoding);
                } catch (UnsupportedEncodingException uee) {
                    throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
                }
            }

            @Override
            protected VolleyError parseNetworkError(VolleyError volleyError) {
                String json;
                if (volleyError.networkResponse != null && volleyError.networkResponse.data != null) {
                    try {
                        json = new String(volleyError.networkResponse.data,
                                HttpHeaderParser.parseCharset(volleyError.networkResponse.headers));
                    } catch (UnsupportedEncodingException e) {
                        return new VolleyError(e.getMessage());
                    }
                    return new VolleyError(json);
                }
                return volleyError;
            }
        };

        queue.add(stringRequest);
BNK
  • 23,994
  • 8
  • 77
  • 87
  • Post server's specs/requirements, or screenshot of using Postman/HttpRequester to test with it. 500 is internal server error, debug server app. – BNK Nov 10 '15 at 22:17
0

Volley don't support Protocol SOAP

David Hackro
  • 3,652
  • 6
  • 41
  • 61