使用 Soap 服务:如何从 Android 发送对此 Web 服务的请求

发布于 2024-11-29 23:42:49 字数 5415 浏览 0 评论 0原文

        This is my coding:

            private static final String METHOD_NAME ="total";

             private static final String NAMESPACE ="http://tempuri.org/";
             private static final String SOAP_ACTION ="http://tempuri.org/total";
             private static final String URL ="http://10.0.2.2:1743/Service1.asmx";
             private TextView tv;
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                tv = (TextView) findViewById(R.id.tv);
                tv.setText(ws());
            }
            private String ws() {
                String result = "";
                try {

                    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);    

                System.out.println("ohaih");
                    request.addProperty("Name","chris");
                    request.addProperty("num1",5);
                    request.addProperty("num2",5);

                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                            SoapEnvelope.VER11);

                    envelope.dotNet = true;

                    envelope.setOutputSoapObject(request);


                    HttpTransportSE ht = new HttpTransportSE(URL);

                    ht.call(SOAP_ACTION, envelope);
        System.out.println("dfdjssf");

                    if(envelope.getResponse()!=null){
                    //SoapObject response = (SoapObject)envelope.bodyIn;
                        Object response = envelope.getResponse();
                        result = response.toString();
                    }

                } catch (Exception e) {
                    result = e.getMessage();
                }
                return result;
            }
        }


    webservice coding:

       <System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
        <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
        <ToolboxItem(False)> _
        Public Class Service1
            Inherits System.Web.Services.WebService
            Public Class InfoRequest

                Private NameField As String
                Private num1field As Integer
                Private num2field As Integer
                '''<remarks/>
                Public Property Name() As String
                    Get
                        Return Me.NameField
                    End Get
                    Set(ByVal value As String)
                        Me.NameField = value
                    End Set
                End Property
                '''<remarks/>
                Public Property num1() As Integer
                    Get
                        Return Me.num1field
                    End Get
                    Set(ByVal value As Integer)
                        Me.num1field = value
                    End Set
                End Property
                '''<remarks/>
                Public Property num2() As Integer
                    Get
                        Return Me.num2field
                    End Get
                    Set(ByVal value As Integer)
                        Me.num2field = value
                    End Set
                End Property

            End Class
            Public Class InfoResponse

                Private totalField As Integer

                '''<remarks/>
                Public Property total() As Integer
                    Get
                        Return Me.totalField
                    End Get
                    Set(ByVal value As Integer)
                        Me.totalField = value
                    End Set
                End Property

            End Class
            <WebMethod()> _
            Public Function total(ByVal validationRequest As InfoRequest) As InfoResponse
                Dim resp As New InfoResponse
                Dim req = validationRequest

                Dim name As String = req.Name
                Dim num1 As Integer = req.num1
                Dim num2 As Integer = req.num2
                Dim tot As Integer = num1 + num2
                resp.total = tot
                Return resp
            End Function

        End Class



    WEBSERVICE:



    REQUEST:

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
        <total xmlns="http://tempuri.org/">
          <validationRequest>
            <Name>string</Name>
            <num1>int</num1>
            <num2>int</num2>
          </validationRequest>
        </total>
      </soap:Body>
    </soap:Envelope>


RESPONCE:


?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <HelloWorldResponse xmlns="http://tempuri.org/">
      <HelloWorldResult>int</HelloWorldResult>
    </HelloWorldResponse>
  </soap:Body>
</soap:Envelope>
        This is my coding:

            private static final String METHOD_NAME ="total";

             private static final String NAMESPACE ="http://tempuri.org/";
             private static final String SOAP_ACTION ="http://tempuri.org/total";
             private static final String URL ="http://10.0.2.2:1743/Service1.asmx";
             private TextView tv;
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                tv = (TextView) findViewById(R.id.tv);
                tv.setText(ws());
            }
            private String ws() {
                String result = "";
                try {

                    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);    

                System.out.println("ohaih");
                    request.addProperty("Name","chris");
                    request.addProperty("num1",5);
                    request.addProperty("num2",5);

                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                            SoapEnvelope.VER11);

                    envelope.dotNet = true;

                    envelope.setOutputSoapObject(request);


                    HttpTransportSE ht = new HttpTransportSE(URL);

                    ht.call(SOAP_ACTION, envelope);
        System.out.println("dfdjssf");

                    if(envelope.getResponse()!=null){
                    //SoapObject response = (SoapObject)envelope.bodyIn;
                        Object response = envelope.getResponse();
                        result = response.toString();
                    }

                } catch (Exception e) {
                    result = e.getMessage();
                }
                return result;
            }
        }


    webservice coding:

       <System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
        <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
        <ToolboxItem(False)> _
        Public Class Service1
            Inherits System.Web.Services.WebService
            Public Class InfoRequest

                Private NameField As String
                Private num1field As Integer
                Private num2field As Integer
                '''<remarks/>
                Public Property Name() As String
                    Get
                        Return Me.NameField
                    End Get
                    Set(ByVal value As String)
                        Me.NameField = value
                    End Set
                End Property
                '''<remarks/>
                Public Property num1() As Integer
                    Get
                        Return Me.num1field
                    End Get
                    Set(ByVal value As Integer)
                        Me.num1field = value
                    End Set
                End Property
                '''<remarks/>
                Public Property num2() As Integer
                    Get
                        Return Me.num2field
                    End Get
                    Set(ByVal value As Integer)
                        Me.num2field = value
                    End Set
                End Property

            End Class
            Public Class InfoResponse

                Private totalField As Integer

                '''<remarks/>
                Public Property total() As Integer
                    Get
                        Return Me.totalField
                    End Get
                    Set(ByVal value As Integer)
                        Me.totalField = value
                    End Set
                End Property

            End Class
            <WebMethod()> _
            Public Function total(ByVal validationRequest As InfoRequest) As InfoResponse
                Dim resp As New InfoResponse
                Dim req = validationRequest

                Dim name As String = req.Name
                Dim num1 As Integer = req.num1
                Dim num2 As Integer = req.num2
                Dim tot As Integer = num1 + num2
                resp.total = tot
                Return resp
            End Function

        End Class



    WEBSERVICE:



    REQUEST:

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
        <total xmlns="http://tempuri.org/">
          <validationRequest>
            <Name>string</Name>
            <num1>int</num1>
            <num2>int</num2>
          </validationRequest>
        </total>
      </soap:Body>
    </soap:Envelope>


RESPONCE:


?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <HelloWorldResponse xmlns="http://tempuri.org/">
      <HelloWorldResult>int</HelloWorldResult>
    </HelloWorldResponse>
  </soap:Body>
</soap:Envelope>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春有你 2024-12-06 23:42:49

在您的 SOAP 请求中,
是一种复杂的请求类型,您根本无法像通常对原始类型那样在 request 中添加属性。

您需要创建一个类,它扩展 Vector实现 KVMSerialized

public class ValidationReq extends Vector<String> implements KvmSerializable {


        @Override
        public Object getProperty(int arg0) {
                return this.get(arg0);
        }

        @Override
        public int getPropertyCount() {
                return this.size();
        }

        @Override
        public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
                arg2.name = "string";
                arg2.type = PropertyInfo.STRING_CLASS;
        }

        @Override
        public void setProperty(int arg0, Object arg1) {
                this.add(arg1.toString());
        }

}

您的类将包含您要发送的参数,并且您需要将类的对象添加到请求中。

示例代码段可以在官方 KSOAP 文档< /strong>

In your SOAP request,
<validationRequest> is a complex request type and you simply cannot add the property in the request, as usually done for primitive types.

You need to create a class which extends Vector and implements KVMSerializable.

public class ValidationReq extends Vector<String> implements KvmSerializable {


        @Override
        public Object getProperty(int arg0) {
                return this.get(arg0);
        }

        @Override
        public int getPropertyCount() {
                return this.size();
        }

        @Override
        public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
                arg2.name = "string";
                arg2.type = PropertyInfo.STRING_CLASS;
        }

        @Override
        public void setProperty(int arg0, Object arg1) {
                this.add(arg1.toString());
        }

}

Your class would contain the parameters that you want to send and you need to add the object of the class to the request.

Sample code snippet can be found in the official KSOAP documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文