2013-07-29 101 views
10

我正在開發需要連接web服務以進行響應的android web應用程序。如何將本地xml文件轉換爲org.ksoap2.serialization.SoapObject?

我正在使用kSOAP進行Web服務調用過程。 [kSOAP是一個SOAP web服務客戶端庫,用於約束Java環境,如Applets或J2ME應用程序。]

如果我將響應的xml保存到本地目錄中,例如。 /mnt/sdcard/appData/config.xml然後當我詢問Web服務請求時,首先它會檢查本地文件是否存在,然後將該文件視爲已回覆文件,否則連接到服務器。

該過程減少了響應時間並提高了應用程序的效率。

是否可以將它('config.xml')轉換爲SOAP對象?如何?

考慮我的XML本地文件如下:

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> 

當前的代碼如下所示:

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"); 
     } 

} 

回答

8

你可以擴展HttpTransportSE類並覆蓋方法call是這樣的:

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

感謝它對我來說工作正常。 –

1

的問題是如何將XML文件轉換爲SoapObject。所以如何讓你的輸入xml信封進入ksoap2調用。

儘管這不是它的預期用途,但實際上可以在HttpTransportSE類中使用這種方法!

有一個方法「parseResponse」,它接受信封和輸入流(您的xml文件)並更新信封輸入標題和正文。但巧妙的是,你可以將它們複製到outHeader和outBody字段中,然後映射字段的所有努力都會消失。

 @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); 

}