使用kso​​ap2从Android访问WebService

时间:2013-03-13 11:28:44

标签: android web-services soap ksoap2 android-ksoap2

搜索了一整天后,我仍然无法找到解决问题的方法。

我有一个WebService,它给了我以下WSDL file,我上传到pastebin因为它真的很大。

首先,我想实现ping函数,您可以使用字符串值调用该函数,并将字符串值返回给客户端。我认为相关部分是:

<wsdl:operation name="ping">
<wsdl:input message="tns:ping" name="ping"></wsdl:input>
<wsdl:output message="tns:pingResponse" name="pingResponse"></wsdl:output>
<wsdl:fault message="tns:ServerException" name="ServerException"></wsdl:fault>
<wsdl:fault message="tns:UserException" name="UserException"></wsdl:fault>
</wsdl:operation>

<xs:complexType name="ping">
<xs:sequence>
<xs:element minOccurs="0" name="in" type="xs:string"/>
</xs:sequence>
</xs:complexType>

无论如何,如果您能查看实际的wsdl文件并告诉我它们是否真的是它的相关部分,那将是非常好的。

所以,要使用Android访问它我正在使用kso​​ap2。我的代码如下:

SoapObject request = new SoapObject("http://shared.bimserver.org/", "ping");
request.addProperty("in", "Hallo Welt");

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

HttpTransportSE httpTransport = new HttpTransportSE("http://10.0.0.5:8082/soap");
httpTransport.debug = true;
httpTransport.call("http://shared.bimserver.org/ping", envelope);

这会创建以下请求:

<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"><v:Header /><v:Body><ping xmlns="http://shared.bimserver.org/" id="o0" c:root="1"><in i:type="d:string">Hallo Welt</in></ping></v:Body></v:Envelope>

但我得到了这样的答复:

<soap:Body><soap:Fault><faultcode>soap:Client</faultcode><faultstring>Unmarshalling Error: unexpected element (uri:"http://shared.bimserver.org/", local:"in"). Expected elements are &lt;{}in> </faultstring></soap:Fault></soap:Body>

所以我希望,参数“in”必须没有命名空间。我尝试了不同的东西:

PropertyInfo info = new PropertyInfo();
info.setName("in");
info.setNamespace("");
info.setValue("Hallo Welt");
request.addProperty(info);

但这并没有改变请求或响应。​​

我还从soap对象中删除了命名空间:

SoapObject request = new SoapObject("", "ping");

这给了我以下要求:

<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"><v:Header /><v:Body><ping xmlns="" id="o0" c:root="1"><in i:type="d:string">Hallo Welt</in></ping></v:Body></v:Envelope>

以下回复:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><soap:Fault><faultcode>soap:Client</faultcode><faultstring>Unexpected wrapper element ping found.   Expected {http://shared.bimserver.org/}ping.</faultstring></soap:Fault></soap:Body></soap:Envelope>

所以看来,参数“in”必须没有命名空间,但ping请求必须有一个。

请帮帮我。我尝试过任何我能想象到的东西。

编辑:使用soapUI我发现,响应应该是这样的:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:shar="http://shared.bimserver.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <shar:ping>
         <!--Optional:-->
         <in>Hallo</in>
      </shar:ping>
   </soapenv:Body>
</soapenv:Envelope>

我的请求如下:

<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"><v:Header />
  <v:Body>
    <ping xmlns="http://shared.bimserver.org/">
      <in i:type="d:string">test</in>
    </ping>
  </v:Body>
</v:Envelope>

如何让我的请求看起来像所需的请求?

2 个答案:

答案 0 :(得分:0)

以下是如何请求.Net Web服务,在这里我调用名为“Login”的函数,你可以在函数链接行中显示的soap envelop格式找到你的服务的名称空间,所以在这里“http://www.example.com/ “在命名空间和函数名称id登录。有关详细信息,请查看this链接


    private static final String URL = "http://www.example.com/service.asmx";
    private static final String NAMESPACE = "http://www.example.com/";
    final String SOAP_ACTION = NAMESPACE+"Login";
    final String METHOD_NAME ="Login";
    String response=null;
    SoapObject request=new SoapObject(NAMESPACE,METHOD_NAME);

        request.addProperty("username",user_name);
        request.addProperty("password", password);

        SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setAddAdornments(false);

        envelope.implicitTypes = true;
        envelope.dotNet=true;
        envelope.setOutputSoapObject(request);
        MarshalBase64 marshal = new MarshalBase64();
        marshal.register(envelope);
        HttpTransportSE httpTransport = new HttpTransportSE(URL);
        httpTransport.call(SOAP_ACTION, envelope);
        SoapPrimitive soap_primitive = null;
            soap_primitive=(SoapPrimitive)envelope.getResponse();
            response=String.valueOf(soap_primitive);

答案 1 :(得分:0)

添加属性的命名空间:

PropertyInfo info = new PropertyInfo();
info.setNamespace("http://www.w3.org/2001/XMLSchema-instance");
info.setName("in");
info.setValue("Hallo Welt");
info.setType(PropertyInfo.STRING_CLASS);
request.addProperty(info);