JAVA axis 调用 .net webservice

breezewj 2009-08-27 09:19:01
JAVA axis 调用 .net webservice
AXIS第一次调用 webservice 速度很快没有异常
第二次要么超时 要么一直卡着不动
急····
...全文
139 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
bssoft2010 2011-09-16
  • 打赏
  • 举报
回复
我做了个测试的例子:发给你瞧瞧,顺便指点一下:
第一步:主测试方法TestGetAllStation
import java.util.ArrayList;
import java.util.List;
import org.apache.axis.client.Service;
import com.huawei.ticket.webxml.getalladdr.RequestGetAllAddr;
import org.tempuri.SaleFunction;

public class TestGetAllStation {

/**
* @param args
*/
public static void main(String[] args) {
try {
ArrayList list=null;
RequestGetAllAddr request=new RequestGetAllAddr();
System.out.println("调用GetAllAddr方法开始");
list=request.GetAllAddr("03", "0000");
System.out.println("list:====="+list);
}
catch (Exception e)
{
System.err.println(e.toString());
}
}
}
2.传递参数,并调用主方法SaleFunction()

import java.util.ArrayList;
import com.huawei.ticket.webxml.getalladdr.CAllAddrXML;
import com.huawei.ticket.service.AllAddrIntf;
import org.tempuri.SaleFunction;
import javax.xml.bind.JAXBElement;

public class RequestGetAllAddr {
public ArrayList GetAllAddr(String BusStation,String Guid)
{
ArrayList alladdr_list=null;
String OutputXML="";
String InputXML="";
//生成获取发车点信息的报文
if(BusStation.length()!=0 && Guid.length()!=0)
{
CAllAddrXML creXml=new CAllAddrXML();
InputXML=creXml.createXml(BusStation,Guid);
System.out.println("生成的报文信息InputXML:====="+InputXML);
}
//传递参数并调用主方法SaleFunction()
String resultXML="";
AllAddrIntf alladdrintf=new AllAddrIntf();
alladdr_list=alladdrintf.SaleFunction("GetAllAddr",InputXML,OutputXML,resultXML);
return alladdr_list;
}

}
3、生成报文中的inputxml

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import com.huawei.ticket.search.GetAllAddr;
import org.jdom.input.SAXBuilder;

public class CAllAddrXML {
/*
* 创建获取发车点信息的Inputxml报文
*/
public String createXml(String BusStation,String Guid) {
StringBuffer sb = new StringBuffer();
// 生成报文
sb.append("<Input>");
sb.append("<Count>"+2+"</Count>");
sb.append("<BusStation>"+BusStation+"</BusStation>");
sb.append("<Guid>" +Guid+"</Guid>");
sb.append("</Input>");
return sb.toString();
}


/*
* 解析获取发车点信息的Outputxml报文
*/
public ArrayList parseXML(String OutputXML)
{
//声明解析后的Outputxml结果集
ArrayList<GetAllAddr> alladdrList = new ArrayList<GetAllAddr>();
//获取返回报文的根节点
ByteArrayInputStream bais = null;
try {
bais = new ByteArrayInputStream(OutputXML.getBytes("NewDataSet"));
} catch (UnsupportedEncodingException uee) {
uee.printStackTrace();
}
SAXBuilder builder = new SAXBuilder();
Document read_doc;
//解析返回的报文
try{
read_doc = builder.build(bais);
Element element = read_doc.getRootElement();
List bodyList = element.getChildren("Table");
Iterator it = bodyList.iterator();
while (it.hasNext()) {
//定义GetAllAddr的变量并获取所有的发车点信息包含字段
GetAllAddr all_addr=new GetAllAddr();
Element AllAddrInfo = (Element)it.next();
String str_addr=AllAddrInfo.getChildText("Addr");
String str_addrname=AllAddrInfo.getChildText("AddrName");
String str_busstation=AllAddrInfo.getChildText("BusStation");
String str_busstationname=AllAddrInfo.getChildText("BusStationName");
all_addr.setAddr(str_addr);
all_addr.setAddrName(str_addrname);
all_addr.setBusStation(str_busstation);
all_addr.setBusStationName(str_busstationname);
//将所有的结果储存到alladdrList里面,便于返回结果集
alladdrList.add(all_addr);
}
}catch(Exception e)
{
e.printStackTrace();
}
return alladdrList;
}
}
4、主方法函数

import java.net.URL;
import java.util.ArrayList;
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import com.huawei.ticket.webxml.getalladdr.CAllAddrXML;
import org.apache.axis.encoding.XMLType;
import javax.xml.rpc.ParameterMode;

public class AllAddrIntf {
public ArrayList SaleFunction(String Cmd,String InputXML,String OutputXML,String resultXML)
{
//解析后的输出结果集
ArrayList str_result=null;
try
{
//调用远程的服务器操作
String url="http://10.185.18.67/webservice/Service.svc?wsdl";
//String url="http://lenovo-282d43b3/webservice/Service.svc?wsdl";
String namespace="http://tempuri.org/";

Service service=new Service();
Call call=(Call)service.createCall();
call.setTargetEndpointAddress(new java.net.URL(url));

//调用.NET写的WebService
call.setUseSOAPAction(true);
call.setSOAPActionURI("http://tempuri.org/Sale/" + "SaleFunction");//URI格式:命名空间+方法名

//第二步,设置方法名时,不能只用一个字符串(调JAVA用法),而要用QName,并且构造QName时一定要加上命名空间
call.setOperationName(new QName(namespace,"SaleFunction"));

//第三步,需要传递参数时,一定要写上参数的变量名称,也是用带命名空间的QName表示。加了参数后要设置返回类型(可能只是axis)。
call.addParameter(new QName(namespace,"Cmd"),XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter(new QName(namespace,"InputXML"),XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter(new QName(namespace,"OutputXML"),XMLType.XSD_STRING, ParameterMode.INOUT);
call.addParameter(new QName(namespace,"resultXML"),XMLType.XSD_STRING, ParameterMode.INOUT);
call.setReturnType(XMLType.XSD_STRING);

//执行方法
String result =(String) call.invoke(new Object[] {Cmd,InputXML,OutputXML,resultXML});

//返回结果
System.out.println("OutputXML is=" + OutputXML);
System.out.println("result=" + result);

// 解析返回的String字符串
System.out.println("=======开始解析========");
CAllAddrXML parseXML = new CAllAddrXML();
str_result = parseXML.parseXML(OutputXML);
} catch (Exception e) {
System.err.println(e.toString());
}
return str_result;
}

}
breezewj 2009-08-27
  • 打赏
  • 举报
回复
结贴~
我对不住大家
最后调试出我竟然有一个连接忘关了··
郁闷····
现在已经OK 了···
jiazimo 2009-08-27
  • 打赏
  • 举报
回复
期待、、

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧