访问http://www.webxml.com.cn/zh_cn/web_services_item.aspx?id=494536374B66307964534D3D 可以获取一些外网提供的服务。
例如:天气预报
2500多个城市天气预报 WEB服务 |
Endpoint: http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx ![]() |
Disco: http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?disco ![]() |
WSDL: http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl ![]() |
![]() |
![]() |
复制wsdl里的网址 http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
cmd中输入 wsimport -s . http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
出现了问题,这是由于网站提供的xml有点问题,不过不影响我们使用。
把http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl 里面的内容保存到磁盘上
e:/test/WeatherWS.xml 然后手工改下错误一般删掉出错的地方
然后运行命令 wsimport -s . WeatherWS.xml 生成了 相应的文件夹,把其中的*.class文件删除
复制 文件夹(包含*.java) 到工程中
然后写代码调用webservice服务
查看 网址 中的service ,看结构从地向上看
<wsdl:service name="WeatherWS">
<wsdl:port name="WeatherWSSoap" binding="tns:WeatherWSSoap">
所以写出代码
package cn.com.webxml; import java.util.List; public class AppMain {
public static void main(String [] args)
{
WeatherWS ws=new WeatherWS();
WeatherWSSoap soap=ws.getWeatherWSSoap();
ArrayOfString array=soap.getWeather("杭州", null);
List<String >list=array.getString();
for(String str:list)
{
System.out.println(str);
}
}
}
但是要注意,因为这是我们从e:/test/Weather.xml那里拿来的文件所以需要到
拷过来的WeatherWS.java那里 把e:/test/Weather.xml 都改成
http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
才能保证从网上下载来的数据 而不是从e:/test/Weather.xml那里的数据