Consuming a SOAP Service

170 Views Asked by At

I am trying to connect to the SOAP Service of w3school.com but it is not working.

First I tried it the way I read it in tutorials. This way I got a stream closed exception.

Then I tried to use the KeepAliveHttpsTransportSE class. This way I got an other exception.

org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/} Envelope (position:START_TAG <html lang='en-US'>@3:20 in java.io.InputStreamReader@f298f58) e.printStackTrace();

I have been working on this problem for several days but I don't see what I am doing wrong. Maybe someone can help me.

I have internet permission and I included the library ksoap2-android-assembly-3.6.2-jar-with-dependencies.jar

    public class MainActivity extends AppCompatActivity {
    private Context sContext;


    // SOAP TEST

    private static final String SOAP_ACTION = "https://www.w3schools.com/xml/CelsiusToFahrenheit";
    private static final String METHOD_NAME = "CelsiusToFahrenheit";
    private static final String NAMESPACE = "https://www.w3schools.com/xml/";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        sContext = this;

        myAsyncTask myRequest = new myAsyncTask();
        myRequest.execute();

    }

    private class myAsyncTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {


            String URL = "http://www.w3schools.com/xml/tempconvert.asmx";

            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("Celsius", "48"); // adding method property here serially

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


            KeepAliveHttpsTransportSE keepAliveHttpsTransportSE = new KeepAliveHttpsTransportSE("www.w3schools.com", 443, "", 30000);
            keepAliveHttpsTransportSE.debug = true;

            //HttpTransportSE httpTransport = new HttpTransportSE(URL);
            //httpTransport.debug = true;

            Log.i("mmb Envelop", envelope.toString());


            try {
                //httpTransport.call(SOAP_ACTION, envelope);
                keepAliveHttpsTransportSE.call(SOAP_ACTION, envelope);
            } catch (HttpResponseException e) {
                // TODO Auto-generated catch block
                Log.e("mmb HTTPLOG", e.getMessage());
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.e("mmb IOLOG", e.getMessage());
                // Message: java.io.IOException: Stream closed
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                // TODO Auto-generated catch block
                Log.e("mmb XMLLOG", e.getMessage());
                // Message: org.xmlpull.v1.XmlPullParserException: expected:
                // START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope
                // (position:START_TAG <html lang='en-US'>@3:20 in java.io.InputStreamReader@f298f58)
                e.printStackTrace();
            } //send request

            Object  result = null;
            try {
                result = (Object )envelope.getResponse();
                Log.i("mmb RESPONSE",String.valueOf(result)); // see output in the console
            } catch (SoapFault e) {
                // TODO Auto-generated catch block
                Log.e("mmb SOAPLOG", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }
    }
}
1

There are 1 best solutions below

0
Faiz Mir On

The problem is in w3school services and not in your code..try hitting another services

The error comes up when i hit the services in browser