2012-06-22 102 views
1

我正在開發一個需要訪問第三方dotNet WebService的項目。KSOAP2可以發送複雜類型,但不能收到回

我正在使用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;     
     }   
    } 
} 
+0

您可以分享解決方案嗎?我現在面臨同樣的問題:( –

回答

1

必須在getter和setter方法添加到所有T他實施了KvmSerializable的課程。

+0

什麼都沒有改變 似乎問題是與maaping有關,當標籤本身沒有定義名稱空間時,我仍然不確定。 – Marcus

相關問題