KSOAP2 Library ...将数据发送到asmx webservice时出错

时间:2012-01-11 20:12:53

标签: android soap asmx ksoap2 android-ksoap2

我在Microsoft Azure上安装了ASMX Web服务,我正在尝试将数据发送到Web服务并使用Android应用程序接收输出。为此,我使用的是KSOAP库。

在webservice上,我正在检查字符串是否为空。如果是,我返回错误代码“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应用程序中,我正在发送数据,但是webservice仍然返回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代码之后,webservice返回2405,根据ASMX代码,当任何两个值为null时。

更新:我调试了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\"?>");

请帮助!

非常感谢!

3 个答案:

答案 0 :(得分:2)

我能够清除错误...问题是我在命名空间URI之后没有反斜杠...我没有的原因是反斜杠我得到错误“序列包含否元素“......然而,现在,我有不同的错误......如果我需要帮助,我会在StackOverFlow上发帖...感谢您的慷慨帮助:)

要清楚,命名空间必须在末尾有一个反斜杠,以便服务器接收任何参数。

答案 1 :(得分: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");

尝试使用此代码即可。

答案 2 :(得分:0)

在命名空间它可以帮助你用大写字母H编写http,就像这样

而不是namespace =“http://tempuri.org/”; 试试这个 命名空间= “Http://tempuri.org”;

至少在我的情况下有所帮助。我希望有些人帮忙!

加布里埃尔

相关问题