gpt4 book ai didi

Android ksoap2 命名空间前缀

转载 作者:行者123 更新时间:2023-12-04 16:57:19 26 4
gpt4 key购买 nike

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

传递给 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>

当我在以下两行中指定命名空间时,
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);

这给了我下面的内容,我的肥皂请求被网络服务接受
<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>

关于Android ksoap2 命名空间前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42723016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com