2013-07-29 13 views
10

Sto sviluppando un'applicazione web Android che ha bisogno di connettere il web - servizio per la risposta.Come convertire il file xml locale in org.ksoap2.serialization.SoapObject?

Sto utilizzando kSOAP per il processo di chiamata del servizio web. [kSOAP è una libreria client del servizio Web SOAP per ambienti Java vincolati come applet o applicazioni J2ME.]

Se sto salvando il xml risposto nella directory locale, ad esempio . /mnt/sdcard/appData/config.xml e poi ogni volta che chiedo la richiesta di un servizio web, prima controlla se il file locale è lì, quindi considera quel file come file risposto altrimenti si connette al server.

Questo processo riduce i tempi di risposta e aumenta l'efficienza dell'applicazione.

È possibile convertirlo ('config.xml') in oggetto SOAP? E come?

considerare il mio file locale XML è come di seguito:

config.xml

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<soap:Body> 
<Response xmlns="http://testuser.com/webservices/response"> 
<Result> 
<SName>Test User</SName> 
<UnitDesc>SAMPLE Test </UnitDesc> <RefreshRate>60</RefreshRate> 
<Out> 
<Definition> 
<Code>ABC</Code> 
<Description>(Specific)</Description> 
<Action>true</Action> 
<Despatch>false</Despatch> 
</Definition> 
<Definition> 
<Code>CDE</Code><Description>(Specific)</Description> 
<ActionDate>true</ActionDate> 
</Definition> 
</Out> 
<SampleText> 
<string>Test XML Parsing</string> 
<string>Check how to convert it to SOAP response</string> 
<string>Try if you know</string> 
</SampleText> 
<GeneralData> 
<Pair> 
<Name>AllowRefresh</Name> 
<Value>Y</Value> 
</Pair> 
<Pair> 
<Name>ListOrder</Name> 
<Value>ACCENDING</Value> 
</Pair> 
</GeneralData> 
</Result> 
</Response> 
</soap:Body> 
</soap:Envelope> 

codice attuale è la seguente:

final String CONFIGURATION_FILE="config.xml"; 
File demoDataFile = new File("/mnt/sdcard/appData"); 
boolean fileAvailable=false; 
File[] dataFiles=demoDataFile.listFiles(new FilenameFilter() { 
@Override 
    public boolean accept(File dir, String filename) { 
     return filename.endsWith(".xml"); 
    } 
}); 


for (File file : dataFiles) { 

if(file.getName().equals(CONFIGURATION_FILE)) 
{ 
    fileAvailable=true; 
} 


} 

if(fileAvailable) 
    { 
     //**What to do?** 
    } 
else 
{ 

    //Create the envelope 
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

    //Put request object into the envelope 
    envelope.setOutputSoapObject(request); 

    //Set other properties 
    envelope.encodingStyle = SoapSerializationEnvelope.XSD; 
    envelope.dotNet = true; 
    String method="test"; 

     synchronized (transportLockObject) 
     { 
      String soapAction = "http://testuser.com/webservices/response/"+method; 

      try { 
       transport.call(soapAction, envelope); 
      } catch (SSLHandshakeException she) { 
       she.printStackTrace(); 
       SecurityService.initSSLSocketFactory(ctx); 
       transport.call(soapAction, envelope);    
      } 
     } 

     //Get the response 
     Object response = envelope.getResponse(); 

     //Check if response is available... if yes parse the response 
     if (response != null) 
     { 
      if (sampleResponse != null) 
      { 
       sampleResponse.parse(response); 
      } 
     } 
     else 
     { 
      // Throw no response exception 
      throw new NoResponseException("No response received for " + method + " operation"); 
     } 

} 

risposta

8

è possibile estendere HttpTransportSE di classe e di override metodo call come questo:

public void call(String soapAction, SoapEnvelope envelope) throws IOException, XmlPullParserException 
{ 
    if(localFileAvailable) 
    { 
     InputStream is = new FileInputStream(fileWithXml); 
     parseResponse(envelope, is); 
     is.close(); 
    } 
    else 
    { 
     super.call(soapAction, envelope); 
    } 
} 
+0

Grazie funziona per me. –

1

la domanda era come convertire un file XML per un SoapObject. Quindi, come ottenere la busta xml di input in una chiamata ksoap2.

Il modo per fare ciò è effettivamente disponibile all'interno della classe HttpTransportSE anche se questo non era l'uso previsto!

C'è un metodo "parseResponse" che contiene la busta e un flusso di input (il file xml) e aggiorna l'intestazione e il corpo dell'input della busta. Ma la cosa intelligente è che puoi copiarli nei campi OutHeader e OutBody e poi tutto il duro lavoro dei campi di mappatura va via.

 @Override 
public void call(String soapAction, SoapEnvelope envelope) throws IOException, XmlPullParserException { 
    if (getFileInputStream() != null){ 

     parseResponse(envelope, getFileInputStream()); 
     envelope.bodyOut = envelope.bodyIn; 
     envelope.headerOut = envelope.headerIn; 
     getFileInputStream().close(); 
    } 

    super.call(soapAction,envelope); 

}