Search code examples
androidsoapksoap2

Calling soap operation from Android with no parameters


I have a simple web service operation like so:

@WebService(endpointInterface = "soap.service.sei.HelloWorldSei")
public class Sib {
   public String sayHello() {
      return "Hello World!";
   }
}

I'm using the ksoap2 library for android. In my android activity, I have:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    try {
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive  resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();
            lblResult.setText(resultsRequestSOAP.toString());
        } catch (Exception e) {
            System.out.println("******* THERE WAS AN ERROR ACCESSING THE WEB SERVICE");
            e.printStackTrace();
        }
}

My question is - Since the 'sayHello' operation takes no parameters, do I need to include any 'PropertyInfo' instances?


Solution

  • Yes, it seems that you can. Here's a working android soap client:

        package soap.service.image;
    
        import org.ksoap2.SoapEnvelope;
        import org.ksoap2.serialization.SoapObject;
        import org.ksoap2.serialization.SoapPrimitive;
        import org.ksoap2.serialization.SoapSerializationEnvelope;
        import org.ksoap2.transport.HttpTransportSE;
    
        import android.app.Activity;
        import android.os.Bundle;
    
        public class ImageSoapActivity extends Activity {
    
            private static final String NAMESPACE = "http://image.webservice";
            private static final String URL = "http://10.0.2.2:8080/images?wsdl";
            private static final String METHOD_NAME = "getImage";
            private static final String SOAP_ACTION = "http://image.webservice/getImage";   
    
            /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
                envelope.setOutputSoapObject(request);
                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                try {
                    androidHttpTransport.call(SOAP_ACTION, envelope);
                    SoapPrimitive  resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();
                    System.out.println("****** RESULT: " + resultsRequestSOAP.toString());
                } catch (Exception e) {
                    System.out.println("******* THERE WAS AN ERROR ACCESSING THE WEB SERVICE");
                    e.printStackTrace();
                }        
            }
    }
    

    As you can see, no PropertyInfo objects required ;) On the service side, I'm using Jax-ws. Hope this helps somebody else out. Oh, and it's also worth mentioning, that if you are using Jax-ws to build a soap service, I find that I get the dreaded "cannot find dispatch method" exception in android if I don't use the following elements in the @WebService annotation for my service endpoint interface and service implementation bean: Here is the service endpoint interface

    package soap.service.sei;
    
    import javax.jws.WebService;
    // I get an error in android if I don't include these elements in the
    // @WebService annotation
    @WebService(name = "ImageSei", targetNamespace = "http://image.webservice")
    public interface ImageSei {
        public byte[] getImage();
    }
    

    Here is my service implementation bean

    package soap.service.impl;
    
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.jws.WebService;
    
    import soap.service.sei.ImageSei;
    // I get an error in android if I don't include these elements in the
    // @WebService annotation
    @WebService(endpointInterface = "soap.service.sei.ImageSei", portName = "ImageWSPort",
            serviceName = "ImageWSService", targetNamespace = "http://image.webservice")
    public class ImageSib implements ImageSei {
    
        @Override
        public byte[] getImage() {
            byte[] imageBytes = null;
            try {
                File imageFile = new File("C:\\images\\car.png");
                BufferedImage img = ImageIO.read(imageFile);
                ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
                ImageIO.write(img, "png", baos);
                baos.flush();
                imageBytes = baos.toByteArray();
                baos.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
            System.out.println("Got request");
            return imageBytes;
        }
    
    }
    

    As you can see, this service reads an image as a series of bytes and sends it to the android device as a byte array.

    enter image description here