2011-12-23 90 views
0

我有兩個關於Ksoap2的問題。Android - WSDL/Ksoap2

首先,下面是我的web服務請求的XML示例。

請求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://my-webservice.com/"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <web:getBoard> 
     <!--Optional:--> 
     <language></language> 
     <identification login="" pwd=""/> 
     </web:getBoard> 
    </soapenv:Body> 
</soapenv:Envelope> 

迴應:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Body> 
     <ns2:getBoardResponse xmlns:ns2="http://my-webservice.com/"> 
     <board code="NONE">WhatIWant</board> 
     <board code="HALF">blabla</board> 
     <board code="FULL">blablah</board> 
     <board code="RONL">blablalblba</board> 
     <board code="BRKF">blabla</board> 
     <board code="ALLI">blablablah</board> 
     </ns2:getBoardResponse> 
    </soap:Body> 
</soap:Envelope> 

我必須設置爲DATAS領域: 「語言」, 「登錄」 和 「PWD」,但我不知道使用的好方法......(方法addProperty或setProperty爲現場的「語言」?)

public class CallRefservices { 

    private static final String NAMESPACE = "http://api.myapi.com/test/ws/test"; 
    private static final String URL = "http://api.myapi.com/test/ws/test?wsdl"; 
    private static final String SOAP_ACTION = "getBoard"; 
    private static final String METHOD_NAME = "getBoard"; 

    Boolean getConnection(String login, String pwd) { 
     Boolean checkBoardType = false; 
    try { 
     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
     request.addProperty("login", login); 
     request.addProperty("pwd", pwd); 
     request.addProperty("language", "FR"); 

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

     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
     androidHttpTransport.call(SOAP_ACTION, envelope); 
     SoapObject objetSOAP = (SoapObject)envelope.getResponse(); 
     checkBoardType = this.parserObjet(objetSOAP); 

    } catch (Exception e) { 
     Log.e("getConnection", "", e); 
    } 
    return checkBoardType; 
    } 

    private boolean parserObjet(SoapObject objet) { 
     SoapObject boardObjet = (SoapObject)objet.getProperty("board"); 
     String board = boardObjet.getProperty("NONE").toString(); 

     if (board == "WhatIWant") 
      return true; 
     else 
      return false; 
    } 
} 

我還需要有關分析器。 什麼樣的解析器用於Ksoap2? (DOM,SAX) 我使用的是好方法嗎?

任何幫助表示讚賞! 快樂聖誕btw;)。

編輯:

嗯,我的問題是,發送的XML如下:

[...] <login i:type="d:string">mylogin</login><pwd i:type="d:string">mypassword</pwd><language i:type="d:string">FR</language></n0:getBoardTypes></v:Body></v:Envelope> 

而我需要:

[...] <language></language> 
    <identification login="" pwd=""/> 

:|

編輯2:

嗨,托米斯拉夫,謝謝你的時間和你的答案! 我試過你的解決方案,但它無法正常工作。

我假設送東西,如:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://my.api.com/"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <web:getBoard> 
     <language>FR</language> 
     <identification login="username" pwd="mypwd"/> 
     </web:getBoard> 
    </soapenv:Body> 
</soapenv:Envelope> 

有了您的解決方案,我是這樣的:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://my.api.com/"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <web:getBoard> 
     <language>FR</language> 
     <identification> 
      <login>username</login> 
      <pwd>mypwd</pwd> 
     </identification> 
     </web:getBoard> 
    </soapenv:Body> 
</soapenv:Envelope> 

我想我會創建一個XML模板...:/ !

+0

問題是什麼? addProperty正確添加所有屬性。這段代碼是否失敗?如果是這樣,什麼是例外? – 2011-12-23 14:54:50

+0

那麼Web服務不能識別它? – 2011-12-26 14:10:10

回答

0

所以我假設你的服務方法的簽名是一樣的東西

getBoard(String language, identification ident) // parameter names here must match the param names in the code below 

其中identification是兩個字符串屬性loginpwd類。如果是這樣的話,你可以創建一個新的類並實現KvmSerializable象下面這樣:

public class identification implements KvmSerializable { 
    public String login; 
    public String pwd; 

    public identification (String login, String pwd) { 
     this.login = login; 
     this.pwd = pwd; 
    } 

    public Object getProperty(int i) { 
     switch (i) { 
      case 0: 
       return login; 
      case 1: 
       return pwd; 
     } 
     return null; 
    } 

    public int getPropertyCount() { 
     return 2; 
    } 

    public void setProperty(int i, Object o) { 
     switch (i) { 
      case 0: 
       login = o.toString(); 
       break; 
      case 1: 
       pwd = o.toString(); 
       break; 
     } 
    } 

    public void getPropertyInfo(int i, Hashtable hashtable, PropertyInfo propertyInfo) { 
     switch (i) { 
      case 0: 
       propertyInfo.type = PropertyInfo.STRING_CLASS; 
       propertyInfo.name = "login"; 
       break; 
      case 1: 
       propertyInfo.type = PropertyInfo.STRING_CLASS; 
       propertyInfo.name = "pwd"; 
       break; 
     } 
    } 
} 

然後添加這個屬性類似如下:

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
    request.addProperty("language", "FR"); 
    PropertyInfo info = new PropertyInfo(); 
    info.setName("identification"); // you have to make sure here that the parameter name matches the one in the WSDL, which you havent posted 
    info.setType(identification.class); 
    info.setValue(new identification("username", "password")); 
    request.addProperty(info); 
+0

嗨,托米斯拉夫,謝謝你的時間和你的回答! 我編輯了我的帖子。 再次感謝。 – nick 2011-12-26 14:06:39

+0

通過XML標準,這兩種格式都應該被理解爲相同。我不明白爲什麼它會失敗。 – 2011-12-26 14:14:34