Android:如何使用KSOAP2从头部和主体获取数据

时间:2014-12-10 15:11:20

标签: java android web-services soap ksoap2

在完成一些教程(几乎所有华氏温度到摄氏温度教程)后,我仍然不知道如何实际获取我需要的数据并正确设置请求。 主要问题是进入标题并在<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>

1 个答案:

答案 0 :(得分:0)

给你一个去做你需要做的就是输入正确的值然后把它放在AsyncTask或类似的东西里面。我第一次做肥皂有一个类似的问题,我花了很长时间才弄明白,但手指越过这应该工作!

        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();
            }
        }