如何在使用KSoap2时在代码中创建正确的soap信封(请求xml)?

时间:2014-05-17 12:17:31

标签: android soap wsdl ksoap2 android-ksoap2

这是通过喂食wsdl从SoapUi获得的肥皂请求。

<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的回应(我想在anddroid代码中实现的目标):

  <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或typ: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", "\n\n\nResponse 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代码:

<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 个答案:

答案 0 :(得分:1)

最后,我在经过一些研究后得到了它,并得到了问题的答案。

1)虽然SoapUi发生了<soapenv:Envelope xmlns:soapenv="...." ....>类型响应xml和使用Ksoap2库生成的<v:Envelope xmlns:i="..." ...>类型响应xml的android代码具有不同的外观标记,但它在获取错误方面并不重要。两者都相似。

正如SO question的答案中所提到的,ksoap在SoapEnvelope中具有名称空间的硬编码值。

2)无法识别的操作异常是由MAIN_REQUEST_URL和NAMESPACE中的问题引起的。知道url,namespace和soap_action的正确值有点棘手,至少对于这个空间的初学者来说。

可以通过查看请求/响应xml,wsdl和this nice pictorial example.

来设置这些字段的值

在我的情况下,我不得不改变

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有这个(typ:loginserviceElement)标记包装属性/参数。