Android calls web services published by Axis, Axis2, and Cxf

In the process of calling axis2 in Android to publish the web service, the http500 error has been reported. The axis2 web service is generated by the eclipse plug-in. It is found that it is directly packaged into a war package or run directly in eclipse. When calling from Android, an error will be reported. It must be packaged into an aar package. . If it is not generated by eclipse, but added manually, it can be packaged into a war package for use (refer to the blog post: Building a webservice with axis2 ). As for why the answer has not been found online, and the url to be called is not the same.

The code for calling the web service in Android is as follows:

//调用axis开发的web service
private String callAxisWebService(String name) {
        String result = "";
        String namespace = "http://service.axisdemo.demo.com";
        String url = "http://192.168.1.8:8080/axisdemo/services/HelloService?wsdl";
        String methodName = "sayHello";
        SoapObject soapObject = new SoapObject(namespace, methodName);
        soapObject.addProperty("name", name);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.bodyOut = soapObject;
        HttpTransportSE ht = new HttpTransportSE(url);
        ht.debug = true;
        try {
            ht.call(null, envelope, null);
            SoapObject s1 = (SoapObject) envelope.bodyIn;
            result = s1.getProperty("sayHelloReturn").toString();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (Exception e) {
            Log.e(TAG, e.getLocalizedMessage());
        }
        return result;
    }

    //调用eclipse插件生成的axis2 web service
    private String callAxis2WebService(String name) {
        String result = "";
        String namespace = "http://service.axis2demo.demo.com";
        //这里的url后面没有?wsdl
        String url = "http://192.168.1.8:8080/axis2/services/helloService";
        String methodName = "sayHello";
        SoapObject soapObject = new SoapObject(namespace, methodName);
        soapObject.addProperty("name", name);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.bodyOut = soapObject;
        HttpTransportSE ht = new HttpTransportSE(url);
        ht.debug = true;
        try {
            ht.call(null, envelope);
            SoapObject s1 = (SoapObject) envelope.bodyIn;
            result = s1.getProperty("return").toString();
        } catch (Exception e) {
            Log.e(TAG, e.getLocalizedMessage());
        }
        return result;
    }

    //调用手动配置的axis2 web service
    private String callAxis2WebService2(String name) {
        String result = "";
        String namespace = "http://service.axis2demo2.demo.com";
         //这里的url后面有?wsdl
        String url = "http://192.168.1.8:8080/axis2demo2/services/HelloService?wsdl";
        String methodName = "sayHello";
        SoapObject soapObject = new SoapObject(namespace, methodName);
        soapObject.addProperty("name", name);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.bodyOut = soapObject;
        HttpTransportSE ht = new HttpTransportSE(url);
        ht.debug = true;
        try {
            ht.call(null, envelope);
            SoapObject s1 = (SoapObject) envelope.bodyIn;
            result = s1.getProperty("return").toString();
        } catch (Exception e) {
            Log.e(TAG, e.getLocalizedMessage());
        }
        return result;
    }

    //调用cxf开发的web service
    private String callCxfWebService(String name) {
        String result = "";
        String namespace = "http://service.cxfdemo.demo.com/";
        //在开发中使用了接口,所以?wsdl后面要跟IHelloService.wsdl
        String url = "http://192.168.1.8:8080/cxfdemo/services/HelloServicePort?wsdl=IHelloService.wsdl";
        String methodName = "sayHello";
        SoapObject soapObject = new SoapObject(namespace, methodName);
        soapObject.addProperty("name", name);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.bodyOut = soapObject;
        //envelope.dotNet = true;
        HttpTransportSE ht = new HttpTransportSE(url);
        ht.debug = true;
        try {
            ht.call(null, envelope, null);
            SoapObject s1 = (SoapObject) envelope.bodyIn;
            result = s1.getProperty("return").toString();
        } catch (IOException e) {
            Log.e(TAG, e.getLocalizedMessage());
        } catch (XmlPullParserException e) {
            Log.e(TAG, e.getLocalizedMessage());
        } catch (Exception e) {
            Log.e(TAG, e.getLocalizedMessage());
        }
        return result;
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325280249&siteId=291194637