2012-01-11 66 views
3

我在Microsoft Azure上有一個ASMX webservice安裝程序,我試圖將數據發送到webservice並使用android應用程序接收輸出。爲此,我正在使用KSOAP庫。KSOAP2 Library ...將數據發送到asmx webservice時出錯

在web服務上,我檢查字符串是否爲空。如果是這樣,我返回一個錯誤代碼「2405」

[WebMethod] 
     public string LoginUser(string auth_token, string username) 
     { 
      // All these tests performed, so someone from outside of out application 
      // scope does not attempt to abuse our webservice. 
      #region Check for nulls 
      if (string.IsNullOrEmpty(auth_token)) 
       return "2405"; 
      if (string.IsNullOrEmpty(username)) 
       return "2405"; 
      #endregion 
     } 

在我的Android應用程序,我發送數據,但web服務仍返回2405錯誤代碼,這意味着數據不被髮送。

以下是我的Android代碼:

 SoapObject request = new SoapObject(NAMESPACE, method_name); 

     PropertyInfo pi = new PropertyInfo(); 
     pi.setName("auth_token"); 
     pi.setValue("TestAuth"); 
     request.addProperty(pi); 

     PropertyInfo pe = new PropertyInfo(); 
     pe.setName("username"); 
     pe.setValue("TestUser"); 
     request.addProperty(pe); 

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

     envelope.setOutputSoapObject(request); 
     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
     androidHttpTransport.call(SOAP_ACTION, envelope); 

對不起,我可以爲您提供的命名空間,方法名,網址等,這是對公司的政策,我希望你能理解。 :)

不過,我會再次檢查錯誤。在調用上面的Android代碼之後,web服務返回2405,根據ASMX代碼返回2405,其中任何兩個值都爲空。

更新:我調試了SOAP請求(androidHttpTransport.debug = true)並獲得了requestDump和responseDump的以下結果。

請求轉儲

<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> 
    <LoginUser xmlns="http://tempuri.org" id="o0" c:root="1"> 
     <auth_token i:type="d:string">TestAuth</auth_token> 
     <username i:type="d:string">TestUser</username> 
    </LoginUser> 
    </v:Body> 
</v:Envelope> 

responseDump

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <soap:Body> 
    <LoginUserResponse xmlns="http://tempuri.org/"> 
     <LoginUserResult>2405</LoginUserResult> 
    </LoginUserResponse> 
    </soap:Body> 

</soap:Envelope> 

更新2
這是服務器期望:

<?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> 
    <LoginUser xmlns="http://tempuri.org/"> 
     <auth_token /> 
     <username /> 
    </LoginUser> 
    </soap:Body> 
</soap:Envelope> 

這就是Android應用程序在發送

<?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> 
     <LoginUser xmlns="http://tempuri.org" id="o0" c:root="1"> 
      <auth_token i:type="d:string">TestAuth</auth_token> 
      <username i:type="d:string">TestUser</username> 
     </LoginUser> 
    </v:Body> 
</v:Envelope> 

除了這個,我已經添加了下面一行到Android代碼:

androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 

請幫幫我!

非常感謝!

回答

2

我能夠清除錯誤...問題是,我沒有在命名空間URI後的反斜槓...我沒有這樣做的原因是反斜槓我得到了錯誤「序列包含沒有元素「...但是,現在我有不同的錯誤...如果我需要幫助,我會在StackOverFlow上發佈...感謝您的慷慨幫助:)

要清楚,命名空間必須在末尾有一個反斜槓,以便服務器接收任何參數。

+0

因此,從下次..請粘貼完整的代碼...:P – 2012-01-17 05:50:28

+0

大聲笑。 ..我不認爲那是專業人士嗯.. :) – harsimranb 2012-01-18 06:16:29

0


解決你的問題有兩種方法:一種是通過使用PropertyInfo的方式或其次我的方式:直接添加參數請求參數。

第一方式:

  PropertyInfo pi = new PropertyInfo(); 
      pi.setName("auth_token"); 
      pi.setValue("TestAuth"); 
      pi.setType(String.class); 
      request.addProperty(pi); 

      PropertyInfo pe = new PropertyInfo(); 
      pe.setName("username"); 
      pe.setValue("TestUser"); 
      pe.setType(String.class); 
      request.addProperty(pe); 

第二種方式:

SoapObject request = new SoapObject(NAMESPACE, method_name); 
    request.addProperty("auth_token","TestAuth"); 
    request.addProperty("username","TestUser"); 

試試這個代碼,它會工作。

+0

沒有什麼區別...爲了加強這個問題,我已經把responseDump和requestDump信息放在了...請幫忙! – harsimranb 2012-01-13 19:11:14

+0

我知道它沒有任何區別,但嘗試過或沒有? – 2012-01-14 06:46:35

+0

檢查已編輯的解決方案 – 2012-01-14 07:51:12

0

在命名空間中它可以幫助你編寫與大寫的H的HTTP,這樣

而不是namespace = 「http://tempuri.org/」; 試試這個 namespace =「http://tempuri.org」;

至少在我的情況下已經有所幫助。我希望有一些身體幫助!

加布裏埃爾