2017-03-10 147 views
1

我正在編寫一個Android應用程序,需要使用使用kso​​ap2的wcf SOAP web服務。Android ksoap2命名空間前綴

的數據被傳遞到web服務需要以匹配數據從ASP.Net

ASP.Net SOAP

<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> 

當我指定下列2線命名空間,

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節點上?

回答

0

我得到了我想要作弊......

除了指定一個命名空間,我添加的屬性到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請求是由Web服務接受

<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>