java 对webservice 的基本调用及常见问题的解决方案

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011561335/article/details/79539394

要用webservice,可以有很多技术,我比较常用的事xfire。

不要太复杂,直接贴代码,如下:


		
		String url = "http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl";
		
		Client client = new Client(new URL(url));
		 
		Object[] results = client.invoke("getCountryCityByIp", new Object[] {"27.17.43.14"});  
		 

第一步:获取wsdl地址,网络上有好多免费的地址可以自行搜索。

然后就可以通过client以及具体的参数获取相应的信息。

本次实例为通过ip获取所在地的webservice。参数为:getCountryCityByIp=27.17.43.14  

如果报错且为:服务器无法处理请求。 ---> 未将对象引用设置到对象的实例。
则表示接口有问题或者是方法及参数不对。

如果在获取返回值时候获取到的结果为:[#document: null]

原因是获取到的数据是Document  类型的,那就需要下面的代码来分析数据。

首先通过访问url可以发现,返回值是放在getCountryCityByIpResult中的

代码如下:

Document d = (Document)results[0];   
         NodeList node = d.getElementsByTagName("getCountryCityByIpResult");//此处可获取所有节点循环。
         NodeList n1 = node.item(0).getChildNodes();
         
         for (int i=0;i<n1.getLength();i++){ 
	    	 Node n = n1.item(i);
	         System.out.println(n.getNodeName()+"-->"+n.getFirstChild().getNodeValue());
	     } 

demo下载地址(包含xfire的引用jar):

https://download.csdn.net/download/u011561335/10284632

如有遗漏,请留言

猜你喜欢

转载自blog.csdn.net/u011561335/article/details/79539394