如何在Android中使用Ksoap调用Webservice?

时间:2015-01-31 07:31:13

标签: android web-services ksoap

我在android中调用服务ksoap有问题,因为api服务包含注释属性,所以我不打电话给服务,请帮帮我。

这是api服务

 /api.asmx HTTP/1.1
Host: xxxx
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/RegisterEx"

<?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>
    <RegisterEx xmlns="http://tempuri.org/">
      <objCustReg>
        <CustomerID>int</CustomerID>
        <Firstname>string</Firstname>
        <Lastname>string</Lastname>
        <Email>string</Email>
        <Passwd>string</Passwd>
        <Phone>string</Phone>
        <Address>string</Address>
        <City>string</City>
        <State>string</State>
        <Zipcode>string</Zipcode>
        <Country>string</Country>
        <RegisterDate>string</RegisterDate>
        <RegisteryBy>string</RegisteryBy>
        <Credit />
      </objCustReg>
      <Username>string</Username>
      <Passwd>string</Passwd>
    </RegisterEx>
  </soap:Body>
</soap:Envelope>

这是Code i在android中调用api服务。

              String nameSpace = "http://tempuri.org/";
                String methodName = "RegisterEx";
                // EndPoint
                String endPoint = "http://xxxxx.info/xxxx.asmx";
                // SOAP Action
                String soapAction = "http://tempuri.org/RegisterEx";
                SoapObject rpc = new SoapObject(nameSpace, methodName);
                SoapObject rpc2 = new SoapObject(nameSpace, methodName);
                rpc2.addProperty("CustomerID", "1");
                rpc2.addProperty("Firstname", "boy");
                rpc2.addProperty("Lastname", "boy");
                rpc2.addProperty("Email", "email@gmail.com");
                rpc2.addProperty("Passwd", "123456");
                rpc2.addProperty("Phone", "123465789");
                rpc2.addProperty("Address", "address");
                rpc2.addProperty("City", "city");
                rpc2.addProperty("State", "2222");
                rpc2.addProperty("Zipcode", "2222");
                rpc2.addProperty("RegisterDate", "");
                rpc2.addProperty("RegisteryBy", "");
                rpc2.addProperty("Country", "USA");
                rpc.addProperty("objCustReg", rpc2);
                rpc.addProperty("sWSUsername", "user");
                rpc.addProperty("sWSPasswd", "pass");

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                        SoapEnvelope.VER10);

                envelope.bodyOut = rpc;
                envelope.dotNet = true;
                envelope.setOutputSoapObject(rpc);

                HttpTransportSE transport = new HttpTransportSE(endPoint);
                try {
                    transport.call(soapAction, envelope);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                SoapObject object = (SoapObject) envelope.bodyIn;
                result = object.getProperty(0).toString();

我的代码调用服务返回null,请帮帮我。谢谢你!

1 个答案:

答案 0 :(得分:0)

您必须尝试添加

envelope.implicitTypes=true;

,因为目前objCustReg的类型为RegisterEx。 并改变

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

因为你的信封中的xsi和xsd是2001年的。

你确定,它不会打印异常堆栈跟踪吗?