2

我正在尝试使用 KSOAP 在 Android 上执行以下肥皂请求。它包含一个嵌套的肥皂对象列表。但是,当我收到错误消息时,我一定是做错了什么。

我试图生成的请求如下:

    <?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <SetAttendanceMarks xmlns="http://hostname.net/">
      <strSessionToken>string</strSessionToken>
      <LessonMarks>
        <Count>int</Count>
        <LessonMarks>
          <LessonMark>
            <StudentId>int</StudentId>
            <EventInstanceId>int</EventInstanceId>
            <Mark>string</Mark>
          </LessonMark>
          <LessonMark>
            <StudentId>int</StudentId>
            <EventInstanceId>int</EventInstanceId>
            <Mark>string</Mark>
          </LessonMark>
        </LessonMarks>
      </LessonMarks>
    </SetAttendanceMarks>
  </soap12:Body>
</soap12:Envelope>

我的代码如下:

public boolean setAttendanceMarks(List<Mark> list) throws Exception
{
    boolean result = false;

    String methodName = "SetAttendanceMarks";
    String soapAction = getHost() + "SetAttendanceMarks";

    SoapObject lessMarksN = new SoapObject(getHost(), "LessonMarks");

    for (Mark m : list)
    {
        PropertyInfo smProp =new PropertyInfo();
        smProp.setName("LessonMark");
        smProp.setValue(m);
        smProp.setType(Mark.class);

        lessMarksN.addProperty(smProp);
    }

    PropertyInfo cProp =new PropertyInfo();
    cProp.setName("Count");
    cProp.setValue(list.size());
    cProp.setType(Integer.class);

    SoapObject lessMarks = new SoapObject(getHost(), "LessonMarks");
    lessMarks.addProperty(cProp);
    lessMarks.addSoapObject(lessMarksN);

    PropertyInfo sProp =new PropertyInfo();
    sProp.setName("strSessionToken");
    sProp.setValue(mSession);
    sProp.setType(String.class);

    SoapObject request = new SoapObject(getHost(), methodName); 
    request.addProperty(sProp);
    request.addSoapObject(lessMarks);


    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);

    HttpTransportSE androidHttpTransport = new HttpTransportSE(getURL());
    androidHttpTransport.debug = true;

    androidHttpTransport.call(soapAction, envelope);

    String a = androidHttpTransport.requestDump;
    String b = androidHttpTransport.responseDump;

    SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn; 

    SoapObject res = (SoapObject) resultsRequestSOAP.getProperty(0);

    String resultStr = res.getPropertyAsString("Result");

    if (resultStr.contentEquals("OK"))
    {
        result = true;
    }

    return result;
}

我得到的错误如下:

    <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<soap:Fault>
<soap:Code>
<soap:Value>soap:Sender</soap:Value>
</soap:Code>
<soap:Reason>
<soap:Text xml:lang="en">Server was unable to read request. ---&gt; There is an error in XML document (1, 383). ---&gt; The specified type was not recognized: name='LessonMarks', namespace='http://gsdregapp.net/', at &lt;LessonMarks xmlns='http://gsdregapp.net/'&gt;.</soap:Text>
</soap:Reason>
<soap:Detail />
</soap:Fault>
</soap:Body>
</soap:Envelope>

谁能告诉我我做错了什么?我将不胜感激任何帮助!

4

1 回答 1

1

您的 ws 服务器无法识别您正在使用的名称空间。

SoapObject request = new SoapObject(getHost(), methodName); 

看起来应该是这样的:

SoapObject request = new SoapObject("http://hostname.net/", methodName); 

确保 getHost() 和 getURL() 正在检索相同的主机名,因为主机在 getURL() 上期望的命名空间与 getHost() 发送的不同。

如果您不确定要使用哪个命名空间,请使用 SoapUI 轻松读取 wsdl 并找到正确的命名空间

于 2012-11-01T13:44:56.113 回答