2012-01-11 62 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”;

至少在我的情况下已经有所帮助。我希望有一些身体帮助!

加布里埃尔