Search code examples
androidxml-parsingandroid-volleysimple-framework

How to make XML api request with header, body and params?


I am following using https://gist.github.com/itsalif/6149365 library for making XML requests. It uses Simple-XML for serialising XML to Objects.

I am able to make XML request successfully when there is no header and parameters associate with the SOAP api. However I am unable get response while performing little complex request which consist of header and parameters.

My request format looks like this

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.holidaywebservice.com/HolidayService_v2/">
 <SOAP-ENV:Body>
   <ns1:GetHolidaysAvailable>
     <ns1:countryCode>UnitedStates</ns1:countryCode>
   </ns1:GetHolidaysAvailable>
 </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Code

HashMap<String, String> mapData = new HashMap<>();
final String mRequestBody = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://www.holidaywebservice.com/HolidayService_v2/\">\n" +
        " <SOAP-ENV:Body>\n" +
        "   <ns1:GetHolidaysAvailable>\n" +
        "     <ns1:countryCode>UnitedStates</ns1:countryCode>\n" +
        "   </ns1:GetHolidaysAvailable>\n" +
        " </SOAP-ENV:Body>\n" +
        "</SOAP-ENV:Envelope>";

SimpleXmlRequest dellaRequest = new SimpleXmlRequest<String>
        (Request.Method.POST, "http://www.holidaywebservice.com//HolidayService_v2/HolidayService2.asmx?wsdl", String.class, headerDataMap, bodyDataMap,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        Log.d("BaseActivity", "response = " + response.toString());

                    }
                },
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {

                        Log.d("BaseActivity", "Error:" + error.getMessage());

                    }
                }
        ){
    @Override
    public byte[] getBody() throws AuthFailureError {

        try {
            return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
        } catch (UnsupportedEncodingException uee) {
            VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
                    mRequestBody, "utf-8");
            return null;
        }
    }
};

I am getting 400 response code i.e. BAD REQUEST, state that invalid input is provided e.g validation error or missing data.

This api working fine in Postman rest client. So I am not able to figure out what am I doing wrong.


Solution

  • You can use Volley to call this XML request effectively.

    String url = "http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx";
    
    
    //Volley request
    StringRequest request = new StringRequest(Request.Method.POST, url,
    
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
    
                    Log.d("Response", response);
    
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
    
                    Log.d("Error", error.getMessage());
    
                }
            }) {
    
        @Override
        public String getBodyContentType() {
            // set body content type
            return "text/xml; charset=UTF-8";
        }
    
        @Override
        public byte[] getBody() throws AuthFailureError {
    
            try {
                return reqXML.getBytes("UTF-8");
            } catch (UnsupportedEncodingException uee) {
                // TODO consider if some other action should be taken
                return null;
            }
        }
    
    };
    //
    //Creating a Request Queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    
    //Adding request to the queue
    requestQueue.add(request);