Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can convert local xml file to org.ksoap2.serialization.SoapObject?

I am developing android web application which needs to connect web - service for response.

I am using kSOAP for web service invocation process. [kSOAP is a SOAP web service client library for constrained Java environments such as Applets or J2ME applications.]

If I am saving that responded xml into the local directory, eg. /mnt/sdcard/appData/config.xml and then when ever I ask request for web service, first it will checks if local file is there then consider that file as responded file otherwise connect to the server.

This process reduce response time and increase efficiency of application.

Is it possible to convert it ('config.xml') to SOAP object? And How?

Consider my xml local file is as below:

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>

Current code is shown below:

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

}
like image 805
Sanket Thakkar Avatar asked Jul 29 '13 09:07

Sanket Thakkar


2 Answers

You can extend HttpTransportSE class and override method call like this:

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);
    }
}
like image 91
esentsov Avatar answered Oct 05 '22 22:10

esentsov


The question was how to convert an xml file to a SoapObject. So how to get your input xml envelope into a ksoap2 call.

The way to do this is actually available within the HttpTransportSE class even though this was not its intended use!

There is a method "parseResponse" that takes in the envelope and an input stream(your xml file) and updates the envelope input header and body. But the clever thing is that you can copy these into the outHeader and outBody fields and then all the hard work of mapping fields goes away.

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

}
like image 34
Nigel Bond Avatar answered Oct 06 '22 00:10

Nigel Bond