-->

Android ksoap2 i:type suppression

2019-08-30 02:09发布

问题:

I am trying to figure out how to suppress the i:type tag in the xml output generated by the ksoap2 library. I have tried the answers given in other posts but they have not worked. Does anyone have any ideas?

public static SoapObject createRequest() {

    SoapObject method = new SoapObject(WS_NAMESPACE, WS_METHOD_NAME);
    SoapObject request = new SoapObject("", "Request");

    //Create source credentials object
    SoapObject sourceCredentials = new SoapObject("", "SourceCredentials");

    PropertyInfo sourceNameProp = new PropertyInfo();
    //sourceNameProp.setNamespace(WS_NAMESPACE);
    sourceNameProp.setName("SourceName");
    sourceNameProp.setValue(SOURCENAME);

    PropertyInfo passwordProp = new PropertyInfo();
    //passwordProp.setNamespace(WS_NAMESPACE);
    passwordProp.setName("Password");
    passwordProp.setValue(PASSWORD);

    sourceCredentials.addProperty(sourceNameProp);
    sourceCredentials.addProperty(passwordProp);

    SoapObject siteIds = new SoapObject("","SiteIDs");

    PropertyInfo siteIDProp = new PropertyInfo();
    //siteIDProp.setNamespace(WS_NAMESPACE);
    siteIDProp.setName("int");
    siteIDProp.setValue(SITE_IDS);

    siteIds.addProperty(siteIDProp);

    sourceCredentials.addSoapObject(siteIds);
    request.addSoapObject(sourceCredentials);

    PropertyInfo xMLDetailProp = new PropertyInfo();
    //xMLDetailProp.setNamespace(WS_NAMESPACE);
    xMLDetailProp.setName("XMLDetail");
    xMLDetailProp.setValue("Full");

    PropertyInfo pageSizeProp = new PropertyInfo();
    //pageSizeProp.setNamespace(WS_NAMESPACE);
    pageSizeProp.setName("PageSize");
    pageSizeProp.setValue("10");

    PropertyInfo curPageProp = new PropertyInfo();
    //curPageProp.setNamespace(WS_NAMESPACE);
    curPageProp.setName("CurrentPageIndex");
    curPageProp.setValue("0");

    request.addProperty(xMLDetailProp);
    request.addProperty(pageSizeProp);
    request.addProperty(curPageProp);

    method.addSoapObject(request);

    return method;
}

Then we create the envelope using code snippet below:

    // 1. Create SOAP Envelope using the request
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.implicitTypes = true;
    envelope.setAddAdornments(false);

    envelope.setOutputSoapObject(parameter);

    // 2. Create a HTTP Transport object to send the web service request
    HttpTransportSE httpTransport = new HttpTransportSE(WSDL_URL);
    httpTransport.debug = true; // allows capture of raw request/response in Logcat

    // 3. Make the web service invocation
    httpTransport.call(WS_NAMESPACE + "/" + WS_METHOD_NAME, envelope);

Output XML is below. Problem is the web service does not like the i:type fields on the Request and SourceCredentials nodes and I can't figure out how to suppress them. Can someone please help? Thanks.

    <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>
    <GetLocations xmlns="http://clients.mindbodyonline.com/api/0_5">
      <Request i:type=":Request">
        <SourceCredentials i:type=":SourceCredentials">
          <SourceName i:type="d:string">PilatesonCollins</SourceName>
          <Password i:type="d:string">XGn2PaLeRoD8qoDtrZfODu8j71c=</Password>
          <SiteIDs i:type=":SiteIDs">
            <int i:type="d:int">25755</int>
          </SiteIDs>
        </SourceCredentials>
        <XMLDetail i:type="d:string">Full</XMLDetail>
        <PageSize i:type="d:string">10</PageSize>
        <CurrentPageIndex i:type="d:string">0</CurrentPageIndex>
      </Request>
    </GetLocations>
  </v:Body>
</v:Envelope>

回答1:

You should set right type for each PropertyInfo: propertyInfo.setType(value.getClass());. In conjanction with envelope.implicitTypes = true; it will have effect.