Java调用WebService(axis2)两种方法

 

// 方式一:生成客户端代码调用方式。(使用此方法可以简化很多代码)
 // 
 // 通过插件工具生成客户端代码进行调用。例如:存在一服务为
 // http://127.0.0.1:8000/axis2/services/HelloWorld?wsdl

//保存为HelloWorld.wsdl.

自动生成了代码,包括 HelloWorldProxy.java

public static void main(String[] args){

UploadProxy up = new UploadProxy();
  up.setEndpoint("http://127.0.0.1:70/uploadImg/services/UploadImg");

//图片上传fileinputstream省略
 up.updateImage("", uploadBuffer, 0, 0, "", "", "", "");}
 // 通过插件可以生成SMSSendServiceStub.java。调用的客户端代码如:
 public static void main(String[] args) throws Exception {
  HelloWorldStub stub;
  stub = new HelloWorldStub();
  HelloWorldStub.GetName helloWorld = new HelloWorldStub.GetName();//
  helloWorld.setName("peipan");
  System.out.println(stub.getName(helloWorld).get_return());

 }

//  方式二:使用axis2.rpc.client.RPCServiceClient方式调用。
//  public static void main(String[] args)  throws Exception  {
//  GetWSByAxis2 ws = new GetWSByAxis2();
//  ws.WSUrl = "http://127.0.0.1:8000/axis2/services/HelloWorld";
//  String result;
//  try {
//  result = ws.getStr("getName", "peipan");
//  System.out.println(result);
//  } catch (AxisFault e) {
//  e.printStackTrace();
//  }
//  }

-------------------------------------------------

package test;
import javax.xml.namespace.QName;   

import org.apache.axis2.AxisFault;   
import org.apache.axis2.addressing.EndpointReference;   
import org.apache.axis2.client.Options;   
import org.apache.axis2.rpc.client.RPCServiceClient;   

public class GetWSByAxis2 {
       private static String EndPointUrl;   
       private static String QUrl="http://ws.apache.org/axis2"; 
       private QName opAddEntry;    
       public String WSUrl;   
       public RPCServiceClient setOption() throws AxisFault   
       {   
           RPCServiceClient serviceClient = new RPCServiceClient();   
           Options options = serviceClient.getOptions();   
           EndpointReference targetEPR = new EndpointReference(WSUrl);   
           options.setTo(targetEPR);   
           return serviceClient;   
       }   
          
       public QName getQname(String Option){   
              
           return new QName (QUrl,Option);   
       }   
  //返回String   
       public String getStr(String Option,String why) throws AxisFault   
       {   
           RPCServiceClient serviceClient =this.setOption();    
         
           opAddEntry =this.getQname(Option);   
          
          String str = (String) serviceClient.invokeBlocking(opAddEntry,    
                           new Object[]{why}, new Class[]{String.class })[0];   
          return str;   
      }   
 // 返回一维String数组   
       public String[] getArray(String Option) throws AxisFault   
       {   
           RPCServiceClient serviceClient =this.setOption();    
         
           opAddEntry =this.getQname(Option);   
          
          String[] strArray = (String[]) serviceClient.invokeBlocking(opAddEntry,    
                           new Object[]{}, new Class[]{String[].class })[0];   
          return strArray;   
      }   
       //从WebService中返回一个对象的实例   
      public Object getObject(String Option,Object o) throws AxisFault   
      {    
         RPCServiceClient serviceClient =this.setOption();    
          QName qname=this.getQname(Option);   
          Object object = serviceClient.invokeBlocking(qname, new Object[]{},new Class[]{o.getClass()})[0];   
          return object;   
      }   
         
 /       读者可以自己封装数据类型,如int,byte,float等数据类型   
 }  

 

 

 

 

你可能感兴趣的:(移动通信软件技术,webservice,java,string,object,exception,import)