首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

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

2013-12-15 
急等回复关于axis2参数无法传入public static String target http://webservice.webxml.com.cn/webserv

急等回复关于axis2参数无法传入
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传入怎么样也没有结果
[解决办法]
我用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());

[解决办法]

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();


[解决办法]
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();
}

}

热点排行