Pages

Thursday, May 30, 2013

SOAP client on Android using JiBX

Android has no native support for SOAP web services or even XML binding. Sometimes however, your application may be required to utilize web services. In this post I will show you how to consume SOAP web service using JiBX on Android device. First what we need is JiBX (link). You need to download core library and jibx-ws.

Next step is to generate classes form schema (xsd file) of service with witch you want comunicate. You may find the link to schema in WSDL file or you may be required to extract the schema from WSDL file. Now we can use the code generator which you can find in jibx/lib directory of downloaded achieves. Copy schema.xsd extracted from the service definition file (WSDL) to the jibx/lib directory and run the following command:
java -cp jibx-tools.jar org.jibx.schema.codegen.CodeGen schema.xsd
The generated code will appear. Next you need to compile those files. You may do this using javac compiler or import it into your IDE. The last thing is to add binding information to those class. Generator besides the java source code was generate the binding.xml file. Copy this file to the root of your compilation output folder (where the *.class files are). Open this location in command window and run the command:
[pathToYourClassOutputFolder]>java -jar [pathToJiBXlib]\lib\jibx-bind.jar binding.xml
That's it. You are ready to start developing your android application. The above instructions requires the less configuration of all the known to me methods of creating the bindings with JiBX and doesn't bind to any IDE. You can find more information here and here.
Let's return to Android app. You need to add the binded classes to your project (don't add source java files if you not going to add step to run the binding operation with every compilation). You need also add the following jars (and only them) to your project library:
jibx-run.jar from jibx core
jibx-ws.jar from jibx-ws
The last step is the activity code. Lets assume that the QueryRequest is a class representing the root of one of the operation (it comes from classes generated from schema) and QueryResponse is the class representing the root of response to QueryRequest.
public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new RunWSCommand().execute();
    }

    private class RunWSCommand extends AsyncTask {        

        @Override
        protected Object doInBackground(Object... objects) {
            IBindingFactory fact = null;
            try {
                fact = BindingDirectory.getFactory(QueryRequest.class);
                Client client = new SoapClient("http://web.adress.to.web.service.com/", fact); //client will build the SOAP envelope for us
                QueryRequest query=new QueryRequest();
                query.setName("Mike"); // add the parameter to request.
                return (QueryResponse) client.call(query);
            } catch (Exception e) {
                e.printStackTrace(); 
                return null;
            }
        }
    }
}
No network operation can be run in main thread hence I use the AsyncTask here. The INTERNET permission request must also be find in AndroidManifest.xml file to run this code.

0 komentarze:

Post a Comment