使用SOAP服务

时间:2018-05-07 09:37:54

标签: java android soap android-ksoap2 ksoap

我正在尝试连接到w3school.com的SOAP服务,但它无法正常工作。

首先,我按照教程中的方式尝试了它。这样我得到了stream closed exception

然后我尝试使用KeepAliveHttpsTransportSE类。这样我就得到了另一个异常

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();

我已经在这个问题上工作了好几天,但我看不出我做错了什么。也许有人可以帮助我。

我获得了互联网许可,我加入了库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 个答案:

答案 0 :(得分:0)

问题出在w3school服务中,而不是在你的代码中......遇到其他服务

The error comes up when i hit the services in browser