在没有参数的情况下从Android调用soap操作

时间:2011-07-24 11:53:36

标签: android soap ksoap2

我有一个简单的Web服务操作:

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

我正在使用kso​​ap2库来安卓android。 在我的Android活动中,我有:

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

我的问题是 - 由于'sayHello'操作没有参数,我是否需要包含任何'PropertyInfo'实例?

1 个答案:

答案 0 :(得分:1)

是的,似乎你可以。这是一个有效的android soap客户端:

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

如您所见,不需要PropertyInfo对象;)在服务方面,我使用的是Jax-ws。希望这有助于其他人。哦,还值得一提的是,如果你使用Jax-ws来构建一个soap服务,我发现如果我不在@中使用以下元素,我会在android中获得可怕的“无法找到调度方法”异常我的服务端点接口和服务实现bean的WebService注释:这是服务端点接口

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

这是我的服务实现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;
    }

}

如您所见,此服务将图像作为一系列字节读取,并将其作为字节数组发送到Android设备。

enter image description here