How to consume web service using ksoap2

479 Views Asked by At

I am trying to consume a web service for 2 days now and I failed. Here is my code :

String SOAP_ACTION = "urn:WsTestIntf-IWsTest#Test101";
                String METHOD_NAME = "Test101";
                String NAMESPACE = "urn:WsTestIntf-IWsTest";
                String URL = "http://wsig.127.cc:8465/WsTemp.dll/wsdl/IWsTest";

                String strTemp;

                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                envelope.setOutputSoapObject(request);
                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                try
                {
                    androidHttpTransport.call(SOAP_ACTION, envelope);
                    SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;

                    strTemp = resultsRequestSOAP.toString();


                } catch (Exception e) {
                    strTemp = "Error : " + e.getMessage();
                }

                Toast.makeText(getBaseContext(), strTemp, Toast.LENGTH_SHORT).show();

What is wrong with it? I have place the INTERNET permission in my manifest. The web service is written in Delphi.

1

There are 1 best solutions below

4
Adolf Dsilva On

I had a rough time long back with ksoap2, but have a working client side code.. Hope it helps...

Note: It retries for 3 times, you can remove it if you want..

import org.apache.http.client.ClientProtocolException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;

public class HttpClient {

    private static final String SOAP_ACTION = "http://tempuri.org/doPost";
    private static final String METHOD_NAME = "doPost";
    private static final String NAMESPACE = "http://tempuri.org/";
     private static final String URL = "url"

    private static int timeout = 10000;
    private static final int MAXTRY = 3;

    public String getXmlFromUrl(String xmlData) {
        String xml = null;

        int trycount = 0;
        do {
            try {
                // defaultHttpClient
                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

                request.addProperty("data", xmlData);

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                        SoapEnvelope.VER11);
                envelope.dotNet = true;
                envelope.setOutputSoapObject(request);

                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,
                        timeout);

                try {
                    androidHttpTransport.call(SOAP_ACTION, envelope);
                } catch (XmlPullParserException e) {
                    e.printStackTrace();
                }

                SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
                if (result != null)
                    xml = result.toString();

            } catch (SocketTimeoutException e) {
                Log.e("Network", "timeout");
                trycount++;
                xml = "Request Timeout";
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                trycount = MAXTRY;
            } catch (ClientProtocolException e) {
                e.printStackTrace();
                trycount = MAXTRY;
            } catch (IOException e) {
                e.printStackTrace();
                xml = "Connection Error";
                trycount = MAXTRY;
            } finally {
                if (xml == null)
                    xml = ""; // to avoid exception in while condition
            }
        } while (trycount < MAXTRY && xml.equalsIgnoreCase("Request Timeout"));

        return xml == "" ? "Something Went Wrong Please Try Again!" : xml;
    }
}