Android Ksoap2为嵌套(子)类型设置名称空间

时间:2011-05-12 15:05:26

标签: android wcf soap ksoap2 android-ksoap2

我想将对象SBNInloggBegar发送到WCF Web服务。 SBNInloggBegar包含对象SBPBegarSBPInloggning,后者又包含许多字符串。 SBPInloggning还包含SBPSubjekt,其中包含许多字符串。 我使用KvmSerializable接口序列化了这些类。

我有一个看起来像这样的函数:

private String soap() {
    String returnString = "";

    String NAMESPACE = "Sambruk";
    String METHOD_NAME = "SBAInloggning";
    String SOAP_ACTION = "Sambruk/AuthenticationService/SBAInloggning";
    String URL = "http://exshaerpm.argentum.local/EliasTest/AuthenticationService/AuthenticationService.svc";

    SoapObject soapRequest = new SoapObject(NAMESPACE, METHOD_NAME);

    SBPBegar begar = new SBPBegar();
    begar.setKommun("Skellefteå kommun");

    SBPInloggning inloggning = new SBPInloggning();
    inloggning.setAnvandarnamn("hej");
    inloggning.setLosenord("hopp");

    SBNInloggBegar inloggbegar = new SBNInloggBegar();
    inloggbegar.setBegarData(begar);
    inloggbegar.setInloggningsData(inloggning);

    PropertyInfo prop = new PropertyInfo();
    prop.setName("request");
    prop.setNamespace("http://www.statskontoret.se/sambruk/nyttomeddelanden");
    prop.setType(inloggbegar.getClass());
    prop.setValue(inloggbegar);
    soapRequest.addProperty(prop);

    //soapRequest.addProperty("request", inloggbegar);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); //****
    envelope.dotNet = true;
    envelope.implicitTypes = true;
    envelope.setAddAdornments(false);

    envelope.setOutputSoapObject(soapRequest);

    envelope.addMapping("http://www.statskontoret.se/sambruk/nyttomeddelanden", "", SBPBegar.class);
    envelope.addMapping("http://www.statskontoret.se/sambruk/nyttomeddelanden", "", SBPInloggning.class);
    envelope.addMapping("http://www.statskontoret.se/sambruk/sbpinloggning", "", SBPSubjekt.class);

    HttpTransportSE aht = new HttpTransportSE(URL);
    aht.debug = true;

    try
    {
        aht.call(SOAP_ACTION, envelope);
        //SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
        Object o = envelope.getResponse();
        SBNInloggSvar inloggSvar = new SBNInloggSvar((SoapObject) o);
        returnString = inloggSvar.toString();
    }
    catch(Exception e)
    {
        e.printStackTrace();
        returnString = e.toString();
    }
    return returnString;
}

这是发送的内容:

<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:d="http://www.w3.org/2001/XMLSchema" 
    xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
    <v:Header />
    <v:Body>
        <SBAInloggning xmlns="Sambruk">
            <n0:request                 
                xmlns:n0="http://www.statskontoret.se/sambruk/nyttomeddelanden">
                <BegarData>
                    <Kommun>Skellefte&#229; kommun</Kommun>
                    <AppNamn i:null="true" />
                    <AppVersion i:null="true" />
                    <MaxAntalSvar i:null="true" />
                    <AnropsId i:null="true" />
                    <LastDataVersion i:null="true" />
                </BegarData>
                <Inloggningsdata>
                    <anvandarID i:null="true" />                                
                    <anvandarnamn>hej</anvandarnamn>
                    <organisationsAnvID i:null="true" />
                    <losenord>hopp</losenord>
                    <aktor i:null="true" />
                    <subjekt i:null="true" />
                </Inloggningsdata>
            </n0:request>
        </SBAInloggning>
    </v:Body>
</v:Envelope>

我真的不想为<request>设置命名空间。相反,我希望<request>中的命名空间设置为<BegarData><InloggningsData>。另外,我希望<BegarData>为其子项设置命名空间,<InloggningsData>也是如此:

...
<request xmlns:a="http://www.statskontoret.se/sambruk/nyttomeddelanden"     
    xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <a:BegarData xmlns:b="http://www.statskontoret.se/sambruk/sbpbegar">
        <b:Kommun>test</b:Kommun>
    ...
    </a:BegarData>
    <a:InloggningsData xmlns:b="http://www.statskontoret.se/sambruk/sbpinloggning">
        <b:AnvandarID></b:AnvandarID>
    ...
    </a:InloggningsData>
</request>
...

有没有办法做到这一点?

顺便说一句,我使用的是2.5.4版(来自Google Code网站)。

1 个答案:

答案 0 :(得分:2)

我没有为request声明任何名称空间,但是我添加了一个特定类型的映射,如下所示:

envelope.addMapping("http://www.statskontoret.se/sambruk/nyttomeddelanden", "SBNInloggBegar", SBNInloggBegar.class);
envelope.addMapping("http://www.statskontoret.se/sambruk/sbpbegar", "SBPBegar", SBPBegar.class);
envelope.addMapping("http://www.statskontoret.se/sambruk/sbpinloggning", "SBPInloggning", SBPInloggning.class);
envelope.addMapping("http://www.statskontoret.se/sambruk/sbpsubjekt", "SBPSubjekt", SBPSubjekt.class);

另外,我将implicitTypes设置为true