Ask Me About My WSDL
It has been some time since we have posted some code and we do apologize for that. Ultimately, this post as well as more to come are here to rectify that. We have been making some Android applications as you may have noticed in some previous posts and we want to share some of what we a learned in this post. More specifically, a skeleton template of how to use KSoap2 and WSDL to get results. Check out the code below and let us know what you think!

        // Create strings for the path to the WSDL
        private static String NAMESPACE = "THE SITE OF WHICH WE GO TO";
        private static String METHOD_NAME = "METHOD WE WILL CALL ON WSDL";
        private static String URL = "THE ASMX FILE WE WILL BE REFERENCING";


        // Create SOAP object to receive the information
        final SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);    
        //Add Properties or Parameters here for WSDL Calls 
        final SoapSerializationEnvelope envelope = 
              new SoapSerializationEnvelope(SoapEnvelope.VER12);
        envelope.setOutputSoapObject(request);
        envelope.dotNet = true;
        // Create an Asynchronous task
        new AsyncTask<Void, Void, Boolean>() {
           Object obj;

           @Override
           protected Boolean doInBackground(Void... params) {
                  //here you can do your background network job
               try
                {
                      HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);          
                      androidHttpTransport.call(NAMESPACE + METHOD_NAME, envelope);             
                      obj = envelope.getResponse();

                      return true;
                }
                catch (Exception e) 
                {
                    e.printStackTrace();   
                        return false;                    
                }
           }
           @Override
           protected void onPostExecute(Boolean result) {
            //here you can do your UI job
               if (!result) 
                   // This will run if you get an error
               else {
            	   byte[] data = Base64.decode(obj.toString(), Base64.DEFAULT);
				String s = new String(data);
				try {
					// Do any UI updates in here
				} catch (XmlException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
               }
               super.onPostExecute(result);
           }
       }.execute();

As you can see above, the code is well documented and can be put inside you createBundle android activity method. Let us know if you have any trouble and we will be here to assist in any way we can! It is good to know that the reason we need an asynchronous task is that network jobs are unable to be run on the main thread, and instead have to be created to do all the information grabbing on another thread and leave the user interface to be updated by the main thread. Let us know in the comments below. Until the next Android development, Happy Hacking!

Share This