如何在使用KSoap2时在代码中创建适当的soap信封(request xml)



这是通过提供wsdl从SoapUi获得的soap请求。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:typ="http://wsclient.xyz.com/types/">
   <soapenv:Header/>
   <soapenv:Body>
      <typ:loginserviceElement>
         <typ:username>test.test</typ:username>
         <typ:password>test123</typ:password>
      </typ:loginserviceElement>
   </soapenv:Body>
</soapenv:Envelope>

但是android代码将(从logcat)请求转储为:

<?xml version="1.0" encoding= "UTF-8" ?>
<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>
        <n0:loginservice xmlns:n0="http://wsclient.xyz.com//">
            <username>test.test</username>
            <password>test1234</password>
        </n0:loginservice>
    </v:Body>
</v:Envelope>

我的问题是-这两个xml请求是相同的还是应该互换工作?如果不是,我如何自定义请求以匹配我从SoapUi获得的请求?

正如我得到的回复中提到的:

SoapFault - faultcode: 'env:Client' faultstring: '处理请求时捕获异常:无法识别的操作:{http://wsclient.xyz.com//}loginservice' faultactor: 'null' detail: null

SoapUi的响应(我想在android代码中实现的):

  <env:Body>
      <ns0:loginserviceResponseElement>
         <ns0:result>
            <ns0:logintoken>181210281021ahash</ns0:logintoken>
            <ns0:hrmsid>0000002</ns0:hrmsid>
         </ns0:result>
      </ns0:loginserviceResponseElement>
   </env:Body>
</env:Envelope>

我在SO和其他教程中找到了许多答案,但都没有成功。

我将不胜感激,如果一个参考好的链接可以提供一些如何清楚地描述不同的标签,如v:Envelope或soapenv:Envelope, n0:loginservice或type:loginserviceElement或type:loginserviceElement等

下面是android代码供参考:

public class SoapRequests {
    private static final boolean DEBUG_SOAP_REQUEST_RESPONSE = true;    
    private static final String MAIN_REQUEST_URL = "http://abc.xyz.com/WSClient/WSServiceSoapHttpPort";
    private static final String NAMESPACE = "http://wsclient.xyz.com//";
    private static final String SOAP_ACTION = "http://wsclient.xyz.com//loginservice";
    private static String SESSION_ID;
    private final void testHttpResponse(HttpTransportSE ht) {
        ht.debug = DEBUG_SOAP_REQUEST_RESPONSE;
        if (DEBUG_SOAP_REQUEST_RESPONSE) {
            Log.v("SOAP RETURN", "Request XML:n" + ht.requestDump);
            Log.v("SOAP RETURN", "nnnResponse XML:n" + ht.responseDump);
        }
    }
    public User getUserData(String name, String pwd){       
         User user = null;
         String methodname = "loginservice";
         SoapObject request = new SoapObject(NAMESPACE, methodname);         
         PropertyInfo userName =new PropertyInfo();
         userName.setName("username");
         userName.setValue(name);
         userName.setType(String.class);
         request.addProperty(userName);
         PropertyInfo password =new PropertyInfo();
         password.setName("password");
         password.setValue(pwd);
         password.setType(String.class);
         request.addProperty(password);
         SoapSerializationEnvelope envelope = getSoapSerializationEnvelope(request);
         HttpTransportSE ht = getHttpTransportSE();
         try {
             ht.call(SOAP_ACTION, envelope);
             testHttpResponse(ht);
             SoapPrimitive resultsString = (SoapPrimitive)envelope.getResponse();
             String data = resultsString.toString();
             Log.v("***********RESPONSE*******************", data);
         } catch (SocketTimeoutException t) {
             t.printStackTrace();
         } catch (IOException i) {
             i.printStackTrace();
         } catch (Exception q) {
             q.printStackTrace();
         }
         // some code to set user data
         ....
         return user;
    }
    private SoapSerializationEnvelope getSoapSerializationEnvelope(SoapObject request) {
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = false;
        envelope.implicitTypes = true;
        envelope.setAddAdornments(false);
        envelope.setOutputSoapObject(request);
        return envelope;
    }
    private final HttpTransportSE getHttpTransportSE() {
        HttpTransportSE ht = new HttpTransportSE(Proxy.NO_PROXY,MAIN_REQUEST_URL,60000);
        ht.debug = true;
        ht.setXmlVersionTag("<?xml version="1.0" encoding= "UTF-8" ?>");
        return ht;
    }
}

android code:

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns0="http://wsclient.xyz.com/types/">
    <env:Body>
    <env:Fault>
        <faultcode>env:Client</faultcode>
        <faultstring>Caught exception while handling request: unrecognized operation: {http://wsclient.hrms.com//}loginservice</faultstring>
    </env:Fault>
    </env:Body>
</env:Envelope>

经过更多的研究,我终于让它工作了,并得到了问题的答案。

1)虽然SoapUi生成的<soapenv:Envelope xmlns:soapenv="...." ....>类型响应xml和使用Ksoap2库生成的<v:Envelope xmlns:i="..." ...>类型响应xml的android代码具有不同的查找标签,但在获取错误方面并不显着。

正如在SO问题的答案中提到的,ksoap在SoapEnvelope中为名称空间硬编码了值。

2)不可识别的操作异常是由于MAIN_REQUEST_URL和NAMESPACE的问题。知道url、名称空间和soap_action的正确值有点棘手,至少对于这个领域的初学者来说是这样。

这些字段的值可以通过查看请求/响应xml、wsdl和这个漂亮的图形示例来设置。

在我的例子中,我必须更改

MAIN_REQUEST_URL = "http://abc.xyz.com/WSClient/WSServiceSoapHttpPort";
NAMESPACE = "http://wsclient.xyz.com//";
SOAP_ACTION = "http://wsclient.xyz.com//loginservice";

MAIN_REQUEST_URL = "http://abc.xyz.com/WSClient/WSServiceSoapHttpPort?WSDL";
NAMESPACE = "http://wsclient.xyz.com/types/";
SOAP_ACTION = "http://wsclient.xyz.com//loginservice";

,我还必须更改:

String methodname = "loginservice";

String methodname = "loginserviceElement";

作为请求/响应xml有这个(类型:loginserviceElement)标签包装属性/参数

最新更新