gpt4 book ai didi

java - Android:如何使用 KSOAP2 从 header 和正文获取数据

转载 作者:太空宇宙 更新时间:2023-11-04 14:18:59 25 4
gpt4 key购买 nike

在学习了一些教程(几乎所有华氏温度到摄氏温度的教程)之后,我仍然不知道如何实际获取我需要的数据并正确设置请求。主要问题是进入 header 并为其提供<AccountInfo/>.内的PartnerID。

我还需要在请求正文中为其提供以下参数:

<keyword>string</keyword>
<records>int</records>
<startingRecord>int</startingRecord>
<searchOptions>ID</searchOptions>

谁能解释一下如何实现这一目标?

POST /service/searchapi.asmx HTTP/1.1
Host: uk.company.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://api.company.com/service/SearchByKeyword"

<?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:Header>
<CompanyHeader xmlns="http://api.company.com/service">
<AccountInfo>
<PartnerID>string</PartnerID>
</AccountInfo>
</CompanyHeader>
</soap:Header>
<soap:Body>
<SearchByKeyword xmlns="http://api.company.com/service">
<keyword>string</keyword>
<records>int</records>
<startingRecord>int</startingRecord>
<searchOptions>ID</searchOptions>
</SearchByKeyword>
</soap:Body>
</soap:Envelope>

最佳答案

尝试一下,您所需要做的就是输入正确的值,然后将其放入 AsyncTask 或类似的东西中。我第一次做 SOAP 时遇到了类似的问题,我花了很长时间才弄清楚这一切,但祈祷这应该可行!

        Element[] header = new Element[1];
header[0] = new Element().createElement(NAMESPACE, "CompanyHeader");

Element accountInfo = new Element().createElement(NAMESPACE, "AccountInfo");
header[0].addChild(Node.ELEMENT, accountInfo);

Element apiKey = new Element().createElement(NAMESPACE, "PartnerID");
apiKey.addChild(Node.TEXT, "String");
accountInfo.addChild(Node.ELEMENT, apiKey);

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

PropertyInfo keyword =new PropertyInfo();
keyword.name = "keyword";
keyword.setValue("string");
request.addProperty(keyword);

PropertyInfo records =new PropertyInfo();
records.name = "records";
records.setValue(int);
request.addProperty(records);

PropertyInfo startingRecord =new PropertyInfo();
startingRecord.name = "startingRecord";
startingRecord.setValue(int);
request.addProperty(startingRecord);

PropertyInfo searchOptions =new PropertyInfo();
searchOptions.name = "searchOptions";
searchOptions.setValue(string);
request.addProperty(searchOptions);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope
.VER11);
envelope.dotNet = true;
envelope.implicitTypes = true;
envelope.setAddAdornments(false);
envelope.headerOut = header;
envelope.bodyOut = request;

HttpTransportSE ht = getHttpTransportSE();

try {
ht.call(SOAP_ACTION, envelope);
} catch (HttpResponseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} finally {
try {
if (envelope.getResponse() != null) {
SoapObject result = (SoapObject)envelope.getResponse();
Log.e("Results = ", String.valueOf(results));
}
} catch (SoapFault e) {
e.printStackTrace();
}
}

关于java - Android:如何使用 KSOAP2 从 header 和正文获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27404406/

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