急等回复关于axis2参数无法传入

考拉之爱 2013-12-12 11:57:32
public static String target = "http://webservice.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx";
/**
* @param args
*/
public static void main(String[] args){
try{
// 获得客户端
RPCServiceClient serviceClient = new RPCServiceClient();
// 可以在该对象中设置服务端的验证信息
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference(target);
options.setTo(targetEPR);
options.setAction("http://WebXml.com.cn/getTVstationDataSet");
// 在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,也就是<wsdl:definitions>元素的targetNamespace属性值
QName opAddEntry = new QName("http://WebXml.com.cn/","getTVstationDataSet");
// 参数,如果有多个,继续往后面增加即可,不用指定参数的名称

Object[] opAddEntryArgs = new Object[] {"1"};
// 返回参数类型,这个和axis1有点区别
// invokeBlocking方法有三个参数,其中第一个参数的类型是QName对象,表示要调用的方法名;
// 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[];
// 第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。
// 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}
// 如果被调用的WebService方法没有返回值,应使用RPCServiceClient类的invokeRobust方法,
// 该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同
Class[] classes = new Class[] {OMElement.class};
System.out.println("---------->");
OMElement obj=(OMElement)(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]);
System.out.println("============"+obj.toStringWithConsume());
} catch (Exception e) {
e.printStackTrace();
}



}

以上是代码,其中 Object[] opAddEntryArgs = new Object[] {"1"}; 这个1传入怎么样也没有结果
...全文
1465 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
u011157998 2015-05-27
  • 打赏
  • 举报
回复
可以用生成的axis2客户端代码,我做测试的时候写的,下面的HelloServiceStub,HelloWorldDetail ,HelloWorldDetailResponse,User 类都是客户端生成的代码,入参就是hr.setIn0(u)这句; String url = "http://127.0.0.1:8080/webSer/services/HelloService"; HelloServiceStub stub = new HelloServiceStub(url); HelloWorldDetail hr = new HelloWorldDetail(); User u = new User(); u.setUsername("我擦"); u.setId(2346); hr.setIn0(u); HelloWorldDetailResponse hw = stub.helloWorldDetail(hr); User u2 = hw.getOut(); System.out.println(u2.getUsername());
aj749893248 2015-03-19
  • 打赏
  • 举报
回复
因为使用RPCServiceClient方法修递参数时,没办法对参数进行描述。 Object[] opAddEntryArgs = new Object[] {"1"}; 传递到后台默认的报文格式是 arg0=1; 一般webservice的默认的参数也是arg0,arg1,这样是数据是可以传递到后台的。 但是如果webservice对参数进行了描述如: @WebParam(name = "xmlStr"),这样他需要的参数如下: <message name="queryDeptInfo"><part name="xmlStr" type="xsd:string"/></message> 参数名为xmlStr,而你传进来的为arg0,这样后台接收到的参数就为null了。 你可以为new org.apache.axis.client.Service(); 这种方式可以对参数进行描述的。
考拉之爱 2013-12-13
  • 打赏
  • 举报
回复
正是因为我们要学习的东西都不公开,所以我们更自毕,更加无法让更多人把程序学好!
考拉之爱 2013-12-13
  • 打赏
  • 举报
回复
接着我会把两种能成功调用的方式写在博客中,以方便以后大家能调用。
考拉之爱 2013-12-13
  • 打赏
  • 举报
回复
我用了另外两种方式,一种是stub存根方式,楼上的方式昨天我也封装了一个方法,不过好像用serverclient方式有点小问题!
  • 打赏
  • 举报
回复
axis2也是可以的,昨晚研究了下,可以的。不过不是用RPCServiceClient客户端了,而是ServiceClient了。
// axis2方式
	private static void axis2WebService() {
		try {
			String soapBindingAddress = "http://webservice.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx?wsdl";
			ServiceClient sender = new ServiceClient();
			EndpointReference endpointReference = new EndpointReference(
					soapBindingAddress);
			Options options = new Options();
			options.setAction("http://WebXml.com.cn/getTVstationDataSet");
			options.setTo(endpointReference);
			sender.setOptions(options);
			OMFactory fac = OMAbstractFactory.getOMFactory();
			// 这个和qname差不多,设置命名空间
			OMNamespace omNs = fac.createOMNamespace("http://WebXml.com.cn/",
					"getTVstationDataSet");
			OMElement data = fac.createOMElement("getTVstationDataSet", omNs);
			// 对应参数的节点
			String[] strs = new String[] { "theAreaID" };
			// 参数值
			String[] val = new String[] { "-4" };
			for (int i = 0; i < strs.length; i++) {
				OMElement inner = fac.createOMElement(strs[i], omNs);
				inner.setText(val[i]);
				data.addChild(inner);
			}
			// 发送数据,返回结果
			OMElement result = sender.sendReceive(data);
			System.out.println(result.toString());
		} catch (AxisFault ex) {
			ex.printStackTrace();
		}

	}
考拉之爱 2013-12-12
  • 打赏
  • 举报
回复
希望用过的朋友提供一下思路与方向
考拉之爱 2013-12-12
  • 打赏
  • 举报
回复
没有异常,只是没有数据,这个原因是2参数没有传入
yibo2010 2013-12-12
  • 打赏
  • 举报
回复
检查一下WSDL文件吧 ,看看参数有没有对应起来..
末日哥 2013-12-12
  • 打赏
  • 举报
回复
没异常吗。。
  • 打赏
  • 举报
回复
引用 12 楼 frank_wu 的回复:
有木有用axis2的呀
axis2不如axis1方便。没有规定必须axis2吧。
考拉之爱 2013-12-12
  • 打赏
  • 举报
回复
有木有用axis2的呀
  • 打赏
  • 举报
回复
还有一张情况是用axis2,生成对应的java文件,然后自己就随便用了。
public static void main(String[] args) {
		String[] s = new String[] {
				"-u",
				"http://webservice.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx?wsdl",
				"-o", "client", "-S", "false", "-t" };
		WSDL2Java.main(s);

	}
  • 打赏
  • 举报
回复
引用 7 楼 rui888 的回复:
[quote=引用 5 楼 fangmingshijie 的回复:] 我用httpurlconnection获取过。
String address = "http://webservice.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx";
		URL url = new URL(address);
		HttpURLConnection http = (HttpURLConnection) url.openConnection();
		http.setDoOutput(true);
		http.setDoInput(true);
		http.setRequestMethod("POST");
		http.setUseCaches(false);
		http.setRequestProperty("Content-Type", "text/xml");
		http.connect();

		DataOutputStream out = new DataOutputStream(http.getOutputStream());
		String cityId = "-1";
		String content = "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\"><soap12:Body><getTVstationDataSet xmlns=\"http://WebXml.com.cn/\"><theAreaID>"
				+ cityId
				+ "</theAreaID></getTVstationDataSet></soap12:Body></soap12:Envelope>";
		out.writeBytes(content);

		out.flush();
		out.close();
		BufferedReader reader = new BufferedReader(new InputStreamReader(http
				.getInputStream()));
		String line;
		StringBuffer buffer = new StringBuffer();
		while ((line = reader.readLine()) != null) {
			buffer.append(line);
		}
		reader.close();
		http.disconnect();
		System.out.println(buffer.toString());
回复这么快。上次看你恢复别人的帖子这么获取的。借鉴了。 [/quote]soap访问webservice,基本上都是这样的。呵呵,还有http get或post获取,都可以参考webservice提供的方法获取。
tony4geek 2013-12-12
  • 打赏
  • 举报
回复
引用 5 楼 fangmingshijie 的回复:
我用httpurlconnection获取过。
String address = "http://webservice.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx";
		URL url = new URL(address);
		HttpURLConnection http = (HttpURLConnection) url.openConnection();
		http.setDoOutput(true);
		http.setDoInput(true);
		http.setRequestMethod("POST");
		http.setUseCaches(false);
		http.setRequestProperty("Content-Type", "text/xml");
		http.connect();

		DataOutputStream out = new DataOutputStream(http.getOutputStream());
		String cityId = "-1";
		String content = "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\"><soap12:Body><getTVstationDataSet xmlns=\"http://WebXml.com.cn/\"><theAreaID>"
				+ cityId
				+ "</theAreaID></getTVstationDataSet></soap12:Body></soap12:Envelope>";
		out.writeBytes(content);

		out.flush();
		out.close();
		BufferedReader reader = new BufferedReader(new InputStreamReader(http
				.getInputStream()));
		String line;
		StringBuffer buffer = new StringBuffer();
		while ((line = reader.readLine()) != null) {
			buffer.append(line);
		}
		reader.close();
		http.disconnect();
		System.out.println(buffer.toString());
回复这么快。上次看你恢复别人的帖子这么获取的。借鉴了。
tony4geek 2013-12-12
  • 打赏
  • 举报
回复

		String s = "http://webservice.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx?op=getTVstationDataSet";
		URL url = new URL(s);
		HttpURLConnection http = (HttpURLConnection) url.openConnection();

		http.setDoOutput(true);
		http.setDoInput(true);
		http.setRequestMethod("POST");
		http.setUseCaches(false);
		http.setRequestProperty("Content-Type", "text/xml");
		http.connect();

		OutputStream out = http.getOutputStream();
		String theAreaID = "16";
		String content = "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><getTVstationDataSet  xmlns=\"http://WebXml.com.cn/\"><theAreaID>"
				+ theAreaID
				+ "</theAreaID></getTVstationDataSet></soap:Body></soap:Envelope>";
		out.write(content.getBytes());

		out.flush();
		out.close();
		BufferedReader reader = new BufferedReader(new InputStreamReader(http
				.getInputStream()));
		String line;
		StringBuffer buffer = new StringBuffer();
		while ((line = reader.readLine()) != null) {
			buffer.append(line);
		}
		reader.close();
		http.disconnect();

  • 打赏
  • 举报
回复
我用httpurlconnection获取过。
String address = "http://webservice.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx";
		URL url = new URL(address);
		HttpURLConnection http = (HttpURLConnection) url.openConnection();
		http.setDoOutput(true);
		http.setDoInput(true);
		http.setRequestMethod("POST");
		http.setUseCaches(false);
		http.setRequestProperty("Content-Type", "text/xml");
		http.connect();

		DataOutputStream out = new DataOutputStream(http.getOutputStream());
		String cityId = "-1";
		String content = "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\"><soap12:Body><getTVstationDataSet xmlns=\"http://WebXml.com.cn/\"><theAreaID>"
				+ cityId
				+ "</theAreaID></getTVstationDataSet></soap12:Body></soap12:Envelope>";
		out.writeBytes(content);

		out.flush();
		out.close();
		BufferedReader reader = new BufferedReader(new InputStreamReader(http
				.getInputStream()));
		String line;
		StringBuffer buffer = new StringBuffer();
		while ((line = reader.readLine()) != null) {
			buffer.append(line);
		}
		reader.close();
		http.disconnect();
		System.out.println(buffer.toString());

81,092

社区成员

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

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