KSOAP2可以发送复杂类型但无法接收它

时间:2012-06-22 21:03:47

标签: web-services java-me ksoap2 java1.4

我正在开发一个需要访问第三方dotNet WebService的项目。

我正在使用kso​​ap2-j2se-full-2.1.2.jar(Java 1.4兼容)。 我不能用java> 1.4(平台限制)。

它使用了许多复杂的数据类型。 有一种特殊方法可以接收复杂对象并返回相同的对象类型(可能已修改)。

序列化工作正常,但反序列化不起作用。 “atributos”标签导致错误。

有没有人知道是否可以在两种方式下使用相同的注册数据类型,或者至少比手工操作更好的方式(我必须为很多方法做)?

很抱歉这条长长的留言。但我必须包括与此相关的所有内容。 对不起草率的代码。这只是一个考验。

非常感谢。

这是请求XML:

<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>
        <iniciarExecucaoProcessoComTicket xmlns="http://tempuri.org/" id="o0" c:root="1">
            <ticketAutenticacao>4FSejSSb5JY8N5Sy+nwYDOxNaBsPhFuyF9Dsws8AiNQvwN0nGOUP8w==</ticketAutenticacao>
            <idProcesso>10074</idProcesso>
            <parametrosIniciais>
                <n0:KeyValue xmlns:n0="http://schemas.datacontract.org/2004/07/MyWS.WS.EXT">
                    <n0:key>pnNome</n0:key>
                    <n0:value i:type="d:string">Person1</n0:value>
                </n0:KeyValue>
                <n1:KeyValue xmlns:n1="http://schemas.datacontract.org/2004/07/MyWS.WS.EXT">
                    <n1:key>pnNumero</n1:key>
                    <n1:value i:type="d:string">7001832700</n1:value>
                </n1:KeyValue>
                <n2:KeyValue xmlns:n2="http://schemas.datacontract.org/2004/07/MyWS.WS.EXT">
                    <n2:key>pnTipoSAP</n2:key>
                    <n2:value i:type="d:string">Z005</n2:value>
                </n2:KeyValue>
                <n3:KeyValue xmlns:n3="http://schemas.datacontract.org/2004/07/MyWS.WS.EXT">
                    <n3:key>CodCanal</n3:key>
                    <n3:value i:type="d:string">5</n3:value>
                </n3:KeyValue>
            </parametrosIniciais>
        </iniciarExecucaoProcessoComTicket>
    </v:Body>
</v:Envelope>

以下是答案XML:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <iniciarExecucaoProcessoComTicketResponse xmlns="http://tempuri.org/">
            <iniciarExecucaoProcessoComTicketResult xmlns:a="http://schemas.datacontract.org/2004/07/MyWS.WS.EXT" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                <a:atributos>
                    <a:KeyValue>
                        <a:key>pnNome</a:key>
                        <a:value xmlns:b="http://www.w3.org/2001/XMLSchema" i:type="b:string">Person 1</a:value>
                    </a:KeyValue>
                    <a:KeyValue>
                        <a:key>pnNumero</a:key>
                        <a:value xmlns:b="http://www.w3.org/2001/XMLSchema" i:type="b:string">7001832700</a:value>
                    </a:KeyValue>
                    <a:KeyValue>
                        <a:key>pnTipoSAP</a:key>
                        <a:value xmlns:b="http://www.w3.org/2001/XMLSchema" i:type="b:string">Z005</a:value>
                    </a:KeyValue>
                    <a:KeyValue>
                        <a:key>CodCanal</a:key>
                        <a:value xmlns:b="http://www.w3.org/2001/XMLSchema" i:type="b:string">5</a:value>
                    </a:KeyValue>
                </a:atributos>
                <a:dominio>0</a:dominio>
                <a:ds xmlns:b="http://schemas.datacontract.org/2004/07/MyWS.DTO" i:nil="true"/>
                <a:idInstanciaProcesso>66806</a:idInstanciaProcesso>
                <a:idProcesso>10074</a:idProcesso>
                <a:instanciaEmExecucao xmlns:b="http://schemas.datacontract.org/2004/07/MyWS.DTO" i:nil="true"/>
                <a:nomeUsuario>7001832700</a:nomeUsuario>
                <a:revisao>15076</a:revisao>
                <a:statusInstanciaProcesso>NaoIniciado</a:statusInstanciaProcesso>
            </iniciarExecucaoProcessoComTicketResult>
        </iniciarExecucaoProcessoComTicketResponse>
    </s:Body>
</s:Envelope>

以下是例外:

Unknown Property: KeyValue
java.lang.RuntimeException: Unknown Property: KeyValue
 at org.ksoap2.serialization.SoapSerializationEnvelope.readSerializable(Unknown Source)
 at org.ksoap2.serialization.SoapSerializationEnvelope.readInstance(Unknown Source)
 at org.ksoap2.serialization.SoapSerializationEnvelope.read(Unknown Source)
 at org.ksoap2.serialization.SoapSerializationEnvelope.readSerializable(Unknown Source)
 at org.ksoap2.serialization.SoapSerializationEnvelope.readInstance(Unknown Source)
 at org.ksoap2.serialization.SoapSerializationEnvelope.read(Unknown Source)
 at org.ksoap2.serialization.SoapSerializationEnvelope.readSerializable(Unknown Source)
 at org.ksoap2.serialization.SoapSerializationEnvelope.readInstance(Unknown Source)
 at org.ksoap2.serialization.SoapSerializationEnvelope.read(Unknown Source)
 at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(Unknown Source)
 at org.ksoap2.SoapEnvelope.parse(Unknown Source)
 at org.ksoap2.transport.Transport.parseResponse(Unknown Source)
 at org.ksoap2.transport.HttpTransportSE.call(Unknown Source)
 at ServicoSegundaVia.iniciarExecucaoProcessoComTicket(ServicoSegundaVia.java:194)
 at ServicoSegundaVia.main(ServicoSegundaVia.java:25)
Exception in thread "main" java.lang.NullPointerException
 at ServicoSegundaVia.main(ServicoSegundaVia.java:30)
Java Result: 1

以下是我正在使用的代码:

// Calling code
public static DadosInstanciaProcessoDTO iniciarExecucaoProcessoComTicket(){
    //Get Ticket
    String ticket = "XYZ"; // Got from somewhere else

    //Conect on Service.......
    String sNAMESPACE   = "http://tempuri.org/";
    String sMETHOD_NAME = "iniciarExecucaoProcessoComTicket";
    String sURL         = "http://192.168.0.197:88/WFPExecutorService.svc";
    String sSOAPAACTION = "http://tempuri.org/IWFPExecutorService/iniciarExecucaoProcessoComTicket";

    SoapObject request = new SoapObject(sNAMESPACE, sMETHOD_NAME);

    request.addProperty("ticketAutenticacao", ticket);
    request.addProperty("idProcesso", "10074");

    ParametrosIniciais parIni = new ParametrosIniciais();
    parIni.add(new KeyValue("pnNome", "Person 1"));
    parIni.add(new KeyValue("pnNumero","7001832700"));
    parIni.add(new KeyValue("pnTipoSAP","Z005"));
    parIni.add(new KeyValue("CodCanal","5"));

    PropertyInfo info = new PropertyInfo();
    info.name = "parametrosIniciais";
    info.type = ParametrosIniciais.class;
    request.addProperty(info, parIni);

    //ENVELOPE
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.implicitTypes = true;
    envelope.setOutputSoapObject(request);

    envelope.addMapping(sNAMESPACE, "iniciarExecucaoProcessoComTicketResponse", IniciarExecucaoProcessoComTicketResponse.class);        
    envelope.addMapping(sNAMESPACE, "iniciarExecucaoProcessoComTicketResult", DadosInstanciaProcessoDTO.class);        
    envelope.addMapping("http://schemas.datacontract.org/2004/07/MyWS.WS.EXT", "atributos", Atributos.class);        
    envelope.addMapping("http://schemas.datacontract.org/2004/07/MyWS.WS.EXT", "KeyValue", KeyValue.class);        

    //Creates a conection, using a String URL..
    HttpTransportSE myHttpTransport = new HttpTransportSE(sURL);
    myHttpTransport.debug = true;

    try {
        myHttpTransport.call(sSOAPAACTION, envelope);
        IniciarExecucaoProcessoComTicketResponse response = (IniciarExecucaoProcessoComTicketResponse)envelope.bodyIn;
        DadosInstanciaProcessoDTO dto = response.getDadosIniExecProc();
        System.out.println(dto.getAtributo("pnNome"));
        return dto;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return null;
}

数据类型:

package data;

import java.util.Hashtable;
import java.util.Vector;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;

public class Atributos extends Vector implements KvmSerializable {

    public Object getProperty(int i) { return this.get(i); }

    public int getPropertyCount() { return this.size(); }

    public void setProperty(int i, Object o) { this.add(o); }

    public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo pi) {
        pi.name = "KeyValue";
        pi.namespace = "http://schemas.datacontract.org/2004/07/MyWS.WS.EXT";
        pi.type = KeyValue.class;
    }
}

-

package data;

import java.util.Hashtable;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;

public class DadosExecucaoDTO  implements KvmSerializable {

    private Atributos atributos = null;
    private String sDominio = "";
    private Object oDs = null;
    private String sIdInstanciaProcesso = "";
    private String sIdProcesso = "";
    private Object oInstanciaEmExecucao = null;
    private String sNomeUsuario = "";
    private String sRevisao = "";
    private String sStatusInstanciaProcesso = "";

    public Object getProperty(int i) {
        switch (i) {
            case 0: return atributos;
            case 1: return sDominio;
            case 2: return oDs;
            case 3: return sIdInstanciaProcesso;                
            case 4: return sIdProcesso;
            case 5: return oInstanciaEmExecucao;
            case 6: return sNomeUsuario;
            case 7: return sRevisao;
            case 8: return sStatusInstanciaProcesso;
        }
        return null;    
    }

    public int getPropertyCount() {
        return 9;
    }

    public void setProperty(int i, Object o) {
        switch (i) {
            case 0: this.atributos = (Atributos)o; break;
            case 1: this.sDominio = o.toString(); break;                
            case 2: this.oDs = o; break;
            case 3: this.sIdInstanciaProcesso = o.toString(); break;                
            case 4: this.sIdProcesso = o.toString(); break;
            case 5: this.oInstanciaEmExecucao = o; break;
            case 6: this.sNomeUsuario = o.toString(); break;
            case 7: this.sRevisao = o.toString(); break;                
            case 8: this.sStatusInstanciaProcesso = o.toString(); break;                 
        }           
    }

    public void getPropertyInfo(int i, Hashtable hshtbl, PropertyInfo pi) {
        switch (i) {
            case 0:
                pi.name = "atributos"; pi.type = Atributos.class;
                pi.namespace = "http://schemas.datacontract.org/2004/07/MyWS.WS.EXT";
                break;
            case 1:
                pi.name = "dominio"; pi.type = PropertyInfo.STRING_CLASS;
                pi.namespace = "http://schemas.datacontract.org/2004/07/MyWS.WS.EXT";
                break; 
            case 2:
                pi.name = "ds"; pi.type = PropertyInfo.OBJECT_CLASS;
                pi.namespace = "http://schemas.datacontract.org/2004/07/MyWS.DTO";
                break;                 
            case 3:
                pi.name = "idInstanciaProcesso"; pi.type = PropertyInfo.STRING_CLASS;
                pi.namespace = "http://schemas.datacontract.org/2004/07/MyWS.WS.EXT";
                break; 
            case 4:
                pi.name = "idProcesso"; pi.type = PropertyInfo.STRING_CLASS;
                pi.namespace = "http://schemas.datacontract.org/2004/07/MyWS.WS.EXT";
                break; 
            case 5:
                pi.name = "instanciaEmExecucao"; pi.type = PropertyInfo.OBJECT_CLASS;
                pi.namespace = "http://schemas.datacontract.org/2004/07/MyWS.DTO";
                break; 
            case 6:
                pi.name = "nomeUsuario"; pi.type = PropertyInfo.STRING_CLASS;
                pi.namespace = "http://schemas.datacontract.org/2004/07/MyWS.WS.EXT";
                break;             
            case 7:
                pi.name = "revisao"; pi.type = PropertyInfo.STRING_CLASS;
                pi.namespace = "http://schemas.datacontract.org/2004/07/MyWS.WS.EXT";
                break; 
            case 8:
                pi.name = "statusInstanciaProcesso"; pi.type = PropertyInfo.STRING_CLASS;
                pi.namespace = "http://schemas.datacontract.org/2004/07/MyWS.WS.EXT";
                break; 
        }        
    }
}

-

package data;

import java.util.Hashtable;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;

public class IniciarExecucaoProcessoComTicketResponse implements KvmSerializable {
    private DadosInstanciaProcessoDTO iniciarExecucaoProcessoComTicketResult;

    public DadosInstanciaProcessoDTO getDadosIniExecProc() {
        return iniciarExecucaoProcessoComTicketResult;
    }

    public Object getProperty(int i) {
        switch (i) {
            case 0: return iniciarExecucaoProcessoComTicketResult;
        }
        return null;
    }

    public int getPropertyCount() {
        return 1;
    }

    public void setProperty(int i, Object o) {
        switch (i) {
            case 0: this.iniciarExecucaoProcessoComTicketResult = (DadosInstanciaProcessoDTO)o; break;
        }
    }

    public void getPropertyInfo(int index, Hashtable hshtbl, PropertyInfo info) {
        switch (index) {
            case 0:
                info.type = DadosInstanciaProcessoDTO.class;
                info.name = "iniciarExecucaoProcessoComTicketResult";
                break;
        }
    }
}

-

package data;

import java.util.Hashtable;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;

public class KeyValue implements KvmSerializable {

    private String sKey = "";
    private String sValue = "";

    public KeyValue(String k, String v){
        this.sKey = k; this.sValue = v;
    }

    public Object getProperty(int i) {
        switch (i) {
            case 0: return sKey;
            case 1: return sValue;
        }
        return null;
    }

    public int getPropertyCount() {
        return 2;
    }

    public void setProperty(int i, Object o) {
        switch (i) {
            case 0: this.sKey = o.toString(); break;
            case 1: this.sValue = o.toString(); break;                
        }       
    }

    public void getPropertyInfo(int i, Hashtable hshtbl, PropertyInfo pi) {
        switch (i) {
            case 0:
                pi.name = "key"; pi.namespace = "http://schemas.datacontract.org/2004/07/MyWS.WS.EXT";
                pi.type = PropertyInfo.STRING_CLASS;
                break;
            case 1:
                pi.name = "value"; pi.namespace = "http://schemas.datacontract.org/2004/07/MyWS.WS.EXT";
                pi.type = PropertyInfo.OBJECT_CLASS;
                break;                
        }        
    }
}

1 个答案:

答案 0 :(得分:1)

您必须将getter和setter添加到实现KvmSerializable的所有类中。