Showing posts with label SOAP Webservices. Show all posts
Showing posts with label SOAP Webservices. Show all posts

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)
SOLUTION:
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("os.icloud.com", 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();
        soap.append("\n");
        soap.append("");
// this is a sample data..you have create your own required data  BEGIN
        soap.append(" \n");
        soap.append(" \n");
        soap.append("" + body);
        soap.append("
\n");
        soap.append("
\n");

        /* soap.append(body); */
// END of MEssage Body
        soap.append("");
        Log.i("SOAP Request", ""+soap.toString());
// END of full SOAP request  message
        try {
            HttpEntity entity = new StringEntity(soap.toString(),HTTP.UTF_8);
            httppost.setEntity(entity); 
            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(
                            r_entity.getContent());
                    is.readFully(result);
                }
            }
        } catch (Exception E) {
            Log.i("Exception While Connecting", ""+E.getMessage());
            E.printStackTrace();
        }
       
        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
as
    /** 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();
            xr.setContentHandler(myXMLHandler);
            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.

Android Developers Blog

Ram's shared items