2011-07-24 37 views
1

我有一個簡單的Web服務操作,像這樣:調用SOAP操作不帶參數

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

我使用kso​​ap2庫機器人。 在我的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

是的,你可以。這裏有一個工作的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構建肥皂服務,我發現如果我不在@中使用以下元素,我會在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