Android ksoap2 命名空间前缀

Android ksoap2 namespace prefix

我正在编写一个 Android 应用程序,它需要使用 ksoap2 来使用 wcf SOAP Web 服务。

传递给 Web 服务的数据需要与从 ASP.Net

发送的数据相匹配

ASP.Net肥皂

<v:Envelope xmlns:i="http://www.w3.org/1999/XMLSchema-instance" xmlns:d="http://www.w3.org/1999/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
  <v:Header />
  <v:Body>
    <SaveWaybill xmlns="ClearNamespace">
      <crossWayBill xmlns:n0="http://schemas.datacontract.org/2004/07/CrossWCFService">
        <n0:WayNo>095-090317-1133</n0:WayNo>
        <n0:FinishDateTime>2017-03-09T11:36:03.000Z</n0:FinishDateTime>
        <n0:StartDateTime>2017-03-09T11:33:56.000Z</n0:StartDateTime>
      </crossWayBill>
      <crossWayBillBreakdownItems xmlns:n1="http://schemas.datacontract.org/2004/07/CrossWCFService">
        <n1:CrossService.CrossWaybillBreakdownItem>
          <n1:WayBillItemId>1</n1:WayBillItemId>
          <n1:Quantity>10</n1:Quantity>
        </n1:CrossService.CrossWaybillBreakdownItem>
        <n1:CrossService.CrossWaybillBreakdownItem>
          <n1:WayBillItemId>2</n1:WayBillItemId>
          <n1:Quantity>20</n1:Quantity>
        </n1:CrossService.CrossWaybillBreakdownItem>
        <n1:CrossService.CrossWaybillBreakdownItem>
          <n1:WayBillItemId>3</n1:WayBillItemId>
          <n1:Quantity>30</n1:Quantity>
        </n1:CrossService.CrossWaybillBreakdownItem>
        <n1:CrossService.CrossWaybillBreakdownItem>
          <n1:WayBillItemId>4</n1:WayBillItemId>
          <n1:Quantity>40</n1:Quantity>
        </n1:CrossService.CrossWaybillBreakdownItem>
        <n1:CrossService.CrossWaybillBreakdownItem>
          <n1:WayBillItemId>5</n1:WayBillItemId>
          <n1:Quantity>50</n1:Quantity>
        </n1:CrossService.CrossWaybillBreakdownItem>
      </crossWayBillBreakdownItems>
    </SaveWaybill>
  </v:Body>
</v:Envelope>

Java代码

SoapObject waybill = new SoapObject("", "crossWayBill");
waybill.addProperty("WayNo", WaybillNumber);
waybill.addProperty("FinishDateTime", FinishDateTime);
waybill.addProperty("StartDateTime", StartDateTime);


SoapObject breakdown = new SoapObject("", "crossWayBillBreakdownItems");
SoapObject counter;
for (XmlNode node : xmlNodes) {
    if (node.node.equalsIgnoreCase("WaybillItemCount")) {
        if (node.nodeHasAttriute()) {
            counter = new SoapObject("", "CrossService.CrossWaybillBreakdownItem");
            counter.addProperty("WayBillItemId", WayBillItemId);
            counter.addProperty("Quantity", Quantity);
            breakdown.addSoapObject(counter);
        }
    }
}

SoapSerializationEnvelope envelope;
try {
    String returnValue = "";
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    request.addProperty("crossWayBill", waybill);
    request.addProperty("crossWayBillBreakdownItems", breakdown);

    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);

    envelope.dotNet = true;
    envelope.implicitTypes = true;
    envelope.setOutputSoapObject(request);
    envelope.setAddAdornments(false);

    MarshalDate md = new MarshalDate();
    md.register(envelope);

    HttpTransportSE androidHttpTransport = new HttpTransportSE(URI);
    androidHttpTransport.debug = true;
    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        Log.d(">>----> request dump", androidHttpTransport.requestDump);
        Log.d("<----<< response dump", androidHttpTransport.responseDump);
    } catch (HttpResponseException ex) {
        Log.e("androidHttpTransport", "doInBackground: Error:\n" + ex.getMessage());
    } catch (XmlPullParserException e1) {
        e1.printStackTrace();
    }

    SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
} catch (Exception e) {
    Log.e("GeneralError", "doInBackground: Error:\n" + e.getMessage());
}

使用上面的代码,我得到以下内容:-

<v:Envelope xmlns:i="http://www.w3.org/1999/XMLSchema-instance" xmlns:d="http://www.w3.org/1999/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
  <v:Header />
  <v:Body>
    <SaveWaybill xmlns="ClearNamespace">
      <crossWayBill>
        <WayNo>095-090317-1133</WayNo>
        <StartDateTime>09-Mar-2017 11:33:56</StartDateTime>
        <FinishDateTime>09-Mar-2017 11:36:03</FinishDateTime>
      </crossWayBill>
      <crossWayBillBreakdownItems>
        <CrossService.CrossWaybillBreakdownItem>
          <WayBillId>0</WayBillId>
          <WayBillItemId>1</WayBillItemId>
          <Quantity>0</Quantity>
        </CrossService.CrossWaybillBreakdownItem>
        <CrossService.CrossWaybillBreakdownItem>
          <WayBillId>0</WayBillId>
          <WayBillItemId>2</WayBillItemId>
          <Quantity>0</Quantity>
        </CrossService.CrossWaybillBreakdownItem>
        <CrossService.CrossWaybillBreakdownItem>
          <WayBillId>0</WayBillId>
          <WayBillItemId>3</WayBillItemId>
          <Quantity>0</Quantity>
        </CrossService.CrossWaybillBreakdownItem>
        <CrossService.CrossWaybillBreakdownItem>
          <WayBillId>0</WayBillId>
          <WayBillItemId>4</WayBillItemId>
          <Quantity>0</Quantity>
        </CrossService.CrossWaybillBreakdownItem>
        <CrossService.CrossWaybillBreakdownItem>
          <WayBillId>0</WayBillId>
          <WayBillItemId>5</WayBillItemId>
          <Quantity>0</Quantity>
        </CrossService.CrossWaybillBreakdownItem>
      </crossWayBillBreakdownItems>
    </SaveWaybill>
  </v:Body>
</v:Envelope>

当我在以下两行指定命名空间时,

SoapObject waybill = new SoapObject(CROSS_NAMESPACE, "crossWayBill");
SoapObject breakdown = new SoapObject(CROSS_NAMESPACE, "crossWayBillBreakdownItems");

我得到:-

<v:Envelope xmlns:i="http://www.w3.org/1999/XMLSchema-instance" xmlns:d="http://www.w3.org/1999/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
  <v:Header />
  <v:Body>
    <SaveWaybill xmlns="ClearNamespace">
      <n0:crossWayBill xmlns:n0="http://schemas.datacontract.org/2004/07/CrossWCFService">
        <WayNo>095-090317-1133</WayNo>
        <StartDateTime>09-Mar-2017 11:33:56</StartDateTime>
        <FinishDateTime>09-Mar-2017 11:36:03</FinishDateTime>
      </n0:crossWayBill>
      <n1:crossWayBillBreakdownItems xmlns:n1="http://schemas.datacontract.org/2004/07/CrossWCFService">
        <CrossService.CrossWaybillBreakdownItem>
          <WayBillId>0</WayBillId>
          <WayBillItemId>1</WayBillItemId>
          <Quantity>0</Quantity>
        </CrossService.CrossWaybillBreakdownItem>
        <CrossService.CrossWaybillBreakdownItem>
          <WayBillId>0</WayBillId>
          <WayBillItemId>2</WayBillItemId>
          <Quantity>0</Quantity>
        </CrossService.CrossWaybillBreakdownItem>
        <CrossService.CrossWaybillBreakdownItem>
          <WayBillId>0</WayBillId>
          <WayBillItemId>3</WayBillItemId>
          <Quantity>0</Quantity>
        </CrossService.CrossWaybillBreakdownItem>
        <CrossService.CrossWaybillBreakdownItem>
          <WayBillId>0</WayBillId>
          <WayBillItemId>4</WayBillItemId>
          <Quantity>0</Quantity>
        </CrossService.CrossWaybillBreakdownItem>
        <CrossService.CrossWaybillBreakdownItem>
          <WayBillId>0</WayBillId>
          <WayBillItemId>5</WayBillItemId>
          <Quantity>0</Quantity>
        </CrossService.CrossWaybillBreakdownItem>
      </n1:crossWayBillBreakdownItems>
    </SaveWaybill>
  </v:Body>
</v:Envelope>

如何让名称空间声明 (xmlns:n0="...") 出现在 crossWayBill 和 crossWayBillBreakdownItems 节点上,并使前缀 (n0:) 出现在所有子节点上但不出现在 crossWayBill & crossWayBillBreakdownItems 节点?

我通过作弊得到了我想要的...

我没有指定命名空间,而是向 SoapObject 添加了一个属性并将前缀硬编码到子节点上。

SoapObject waybill = new SoapObject("", "crossWayBill");
waybill.addAttribute("xmlns:n0", "http://schemas.datacontract.org/2004/07/CrossWCFService");
waybill.addProperty("n0:WayNo", WaybillNumber);
waybill.addProperty("n0:FinishDateTime", FinishDateTime);
waybill.addProperty("n0:StartDateTime", StartDateTime);

这给了我下面的信息,网络服务接受了我的 soap 请求

<SaveWaybill xmlns="ClearNamespace">
  <crossWayBill xmlns:n0="http://schemas.datacontract.org/2004/07/CrossWCFService">
    <n0:WayNo>095-090317-1133</n0:WayNo>
    <n0:FinishDateTime>2017-03-09T11:36:03.000Z</n0:FinishDateTime>
    <n0:StartDateTime>2017-03-09T11:33:56.000Z</n0:StartDateTime>
  </crossWayBill>