2013-07-29 5 views
10

मैं एंड्रॉइड वेब एप्लिकेशन विकसित कर रहा हूं जिसे प्रतिक्रिया के लिए वेब सेवा कनेक्ट करने की आवश्यकता है।स्थानीय xml फ़ाइल को org.ksoap2.serialization.ooapObject में कैसे परिवर्तित कर सकते हैं?

मैं वेब सेवा आमंत्रण प्रक्रिया के लिए केएसओएपी का उपयोग कर रहा हूं। मैं बचत कर रहा हूँ अगर वह स्थानीय निर्देशिका में, जैसे एक्सएमएल प्रतिक्रिया व्यक्त [kSOAP ऐसे एप्लेट या जे 2 एमई अनुप्रयोगों के रूप में विवश जावा एनवायरनमेंट के लिए एक सोप वेब सेवा क्लाइंट लाइब्रेरी है।]

। /mnt/sdcard/appData/config.xml और फिर जब भी मैं वेब सेवा के लिए अनुरोध पूछता हूं, तो पहले यह जांच करेगा कि स्थानीय फ़ाइल वहां है या नहीं, तो उस फ़ाइल को प्रतिक्रिया फ़ाइल के रूप में मानें अन्यथा सर्वर से कनेक्ट करें।

यह प्रक्रिया प्रतिक्रिया समय को कम करती है और आवेदन की दक्षता में वृद्धि करती है।

क्या यह इसे ('config.xml') को SOAP ऑब्जेक्ट में परिवर्तित करना संभव है? और कैसे?

पर विचार करें मेरी एक्सएमएल स्थानीय फ़ाइल के रूप में नीचे है:

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

सवाल यह है कि एक SoapObject को एक xml फ़ाइल कन्वर्ट करने के लिए किया गया था। तो अपने इनपुट एक्सएमएल लिफाफा को ksoap2 कॉल में कैसे प्राप्त करें।

ऐसा करने का तरीका वास्तव में HttpTransportSE कक्षा के भीतर उपलब्ध है, भले ही यह इसका इच्छित उपयोग न हो!

एक विधि "parseResponse" है जो लिफाफा और इनपुट स्ट्रीम (आपकी एक्सएमएल फ़ाइल) लेती है और लिफाफा इनपुट हेडर और बॉडी को अपडेट करती है। लेकिन चालाक बात यह है कि आप इन्हें आउटहेडर और आउट बॉडी फ़ील्ड में कॉपी कर सकते हैं और फिर मैपिंग फ़ील्ड के सभी कड़ी मेहनत दूर हो जाती है।

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

} 
संबंधित मुद्दे