2013-07-29 28 views
10

Tôi đang phát triển ứng dụng web dành cho Android cần kết nối dịch vụ web để phản hồi.Làm cách nào để chuyển đổi tệp xml cục bộ thành org.ksoap2.serialization.SoapObject?

Tôi đang sử dụng kSOAP cho quy trình gọi dịch vụ web. [kSOAP là một thư viện máy khách dịch vụ web SOAP cho các môi trường Java bị hạn chế như các ứng dụng Applet hoặc J2ME.]

Nếu tôi tiết kiệm đã trả lời xml vào thư mục cục bộ, ví dụ. /mnt/sdcard/appData/config.xml và sau đó khi tôi yêu cầu dịch vụ web, trước tiên, nó sẽ kiểm tra xem tệp cục bộ có ở đó không và xem tệp đó là tệp trả lời nếu không kết nối với máy chủ.

Quy trình này giảm thời gian phản hồi và tăng hiệu quả của ứng dụng.

Có thể chuyển đổi nó ('config.xml') thành đối tượng SOAP không? Và làm thế nào?

Cân nhắc xml tập tin địa phương của tôi là như sau:

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> 

đang hiện hành được hiển thị dưới đây:

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

} 

Trả lời

8

Bạn có thể mở rộng HttpTransportSE lớp và ghi đè phương pháp call như sau:

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

Cảm ơn nó hoạt động tốt cho tôi. –

1

Câu hỏi đặt ra là làm thế nào để chuyển đổi một tập tin xml để một SoapObject. Vì vậy, làm thế nào để có được phong bì xml đầu vào của bạn vào một cuộc gọi ksoap2.

Cách thực hiện điều này thực sự có sẵn trong lớp HttpTransportSE mặc dù đây không phải là mục đích sử dụng của nó!

Có một phương thức "parseResponse" có trong phong bì và luồng đầu vào (tệp xml của bạn) và cập nhật tiêu đề và nội dung đầu vào phong bì. Nhưng điều thông minh là bạn có thể sao chép chúng vào các trường outHeader và outBody và sau đó tất cả công việc khó khăn của các trường ánh xạ đều biến mất.

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

} 
Các vấn đề liên quan