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<Call> calls = new ArrayList<Call>();

  //构造器实现连接接口中的三个方法,并添加到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<DaibanView> 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接口。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值