Thursday, December 30, 2010

Call SOAP webservices without using any SOAP Libraries in ANDROID

AIM : Accessing SOAP wsdl methods without including any third party SOAP libraries (like KSOAP2)
The below method will call the SOAP server with soap message and returns the ByteArray of response.
private byte[] callSOAPServer() {

        byte[] result = null;
        HttpParams httpParameters = new BasicHttpParams();
        // Set the timeout in milliseconds until a connection is established.
        int timeoutConnection = 15000;
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        // Set the default socket timeout (SO_TIMEOUT)
        // in milliseconds which is the timeout for waiting for data.
        int timeoutSocket = 35000;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
        DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);

         * httpclient.getCredentialsProvider().setCredentials( new
         * AuthScope("", 80, null, "Digest"), new
         * UsernamePasswordCredentials(username, password));
        HttpPost httppost = new HttpPost(SERVER_URL );
        httppost.setHeader("soapaction", SOAP_ACTION);
        httppost.setHeader("Content-Type", "text/xml; charset=utf-8");
        System.out.println("executing request" + httppost.getRequestLine());
//now create a soap request message as follows:
        final StringBuffer soap = new StringBuffer();
// this is a sample have create your own required data  BEGIN
        soap.append(" \n");
        soap.append(" \n");
        soap.append("" + body);

        /* soap.append(body); */
// END of MEssage Body
        Log.i("SOAP Request", ""+soap.toString());
// END of full SOAP request  message
        try {
            HttpEntity entity = new StringEntity(soap.toString(),HTTP.UTF_8);
            HttpResponse response = httpclient.execute(httppost);// calling server
            HttpEntity r_entity = response.getEntity();  //get response
            Log.i("Reponse Header", "Begin...");          // response headers
            Log.i("Reponse Header", "StatusLine:"+response.getStatusLine());
            Header[] headers = response.getAllHeaders();
            for(Header h:headers){
                Log.i("Reponse Header",h.getName() + ": " + h.getValue());
            Log.i("Reponse Header", "END...");
            if (r_entity != null) {       
                result = new byte[(int) r_entity.getContentLength()];  // read the output message
                if (r_entity.isStreaming()) {
                    DataInputStream is = new DataInputStream(
        } catch (Exception E) {
            Log.i("Exception While Connecting", ""+E.getMessage());
        httpclient.getConnectionManager().shutdown(); //shut down the connection
        return result;

2)You have to parse the output of above function returned byteArray.
for example .,
byte[] initReqrepsonse = callSOAPServer(soapBodymessage );
 ByteArrayInputStream bais=new ByteArrayInputStream(initReqrepsonse);
// now parse the xml
    /** Handling XML */
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            /** Create handler to handle XML Tags ( extends DefaultHandler ) */
          // ResponseParser  is XML parser class which will parse the XML output.
            ResponseParser myXMLHandler = new ResponseParser();
            Log.i("XML data", bais.toString());
            xr.parse(new InputSource(bais));

By this way,you can access Any SOAP webservice methods without using any libraries.
please let me know if any corrections required.


  1. Hi Rama,

    Do you think this is the best approach for Web Services access?

  2. Hello,

    I've copied your code in a new Android project but I get some errors on ResponseParser. Eclipse don't recognize it.

    And what kind of message is the soapBodyMessage?

    Thanks in advance

  3. Thanks a lot, you save my day :)

  4. It can either be done by an organization for its student loan, which she sends out twice a month.
    Estimates vary from 7 touches to as many as 90 per cent
    of its revenue from licensing.

    My blog post: Student Loans for People with Bad Credit


Android Developers Blog

Ram's shared items

Amazon Product Ads