java代码实现webservice客户端

老规矩,先贴上代码,再解释

package cn.sh.ideal.portal.interfaces.util.beiji;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;

import net.sf.json.JSONArray;

import org.apache.axis.Constants;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.client.Stub;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.soap.SOAPConstants;

import cn.sh.ideal.portal.cms.content.domain.DaibanView;

public class HyjfClient extends Stub{

  private String endpoint_1="http://************?wsdl"; 
  private String url="http://************************";
  private List calls = new ArrayList();

  //构造器实现连接接口中的三个方法,并添加到list集合
  public HyjfClient() throws ServiceException, MalformedURLException{
    if(service ==null ){
      service =  new Service();
    }
    Call call1=(Call)service.createCall(); 
    call1.setOperationName(new QName(url, "getChannelList")); 
    call1.setEncodingStyle(Constants.URI_SOAP11_ENC);
    call1.setSOAPVersion(SOAPConstants.SOAP11_CONSTANTS);
    call1.setTargetEndpointAddress(new URL(endpoint_1));
    call1.setUseSOAPAction(true);
    call1.setSOAPActionURI("");
    call1.setReturnType(XMLType.XSD_STRING);
    calls.add(call1);//添加getChannelList方法


    Call call2=(Call)service.createCall(); 
    call2.setOperationName(new QName(url, "getTodoList")); 
    call2.setEncodingStyle(Constants.URI_SOAP11_ENC);
    call2.setSOAPVersion(SOAPConstants.SOAP11_CONSTANTS);
    call2.setTargetEndpointAddress(new URL(endpoint_1));
    call2.setUseSOAPAction(true);
    call2.setSOAPActionURI("");
    call2.setReturnType(XMLType.XSD_STRING);
    call2.addParameter(new QName("", "json"), new QName("http://www.w3.org/2001/XMLSchema", "string"), String.class,ParameterMode.IN);
    calls.add(call2);//getTodoList方法


    Call call3=(Call)service.createCall(); 
    call3.setOperationName(new QName(url, "articleOperator")); 
    call3.setEncodingStyle(Constants.URI_SOAP11_ENC);
    call3.setSOAPVersion(SOAPConstants.SOAP11_CONSTANTS);
    call3.setTargetEndpointAddress(new URL(endpoint_1));
    call3.setUseSOAPAction(true);
    call3.setSOAPActionURI("");
    call3.setReturnType(XMLType.XSD_STRING);
    call3.addParameter(new QName("", "json"), new QName("http://www.w3.org/2001/XMLSchema", "string"), String.class,ParameterMode.IN);
    call3.setReturnType(XMLType.XSD_STRING);
    calls.add(call3);//articleOperator方法

  }  


/** * 获得栏目列表 * @return * @throws RemoteException */
  public String getChannelList() throws RemoteException
  {
    return (String) calls.get(0).invoke(new Object[]{});
  }

  /** * 待办 * @param json * @return * @throws RemoteException */
  public String getTodoList(String json) throws RemoteException{
    return (String)calls.get(1).invoke(new Object[]{json});
  }
  /** * 信息添加 * @param json * @return * @throws RemoteException */
  public String articleOperator(String json) throws RemoteException{
      String jsonw=json;
    return (String)calls.get(2).invoke(new Object[]{jsonw});
  }

  public static void main(String[] args) {
    try {
      HyjfClient client = new HyjfClient();
      System.out.println(client.getTodoList("{}"));
      String todo=client.getTodoList("{}");//调用接口方法并传入“{}”参数
      JSONArray todolist = JSONArray.fromObject(todo);//获得接口返回值并且json格式化
      //json格式转java集合
      List list = Arrays.asList((DaibanView[])JSONArray.toArray(todolist,DaibanView.class));
      System.out.println(list.get(0).getContent());

    } catch (MalformedURLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (ServiceException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (RemoteException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }
}

上述代码通过axis实现手写webservice接口。

你可能感兴趣的:(常用案例)