1. 程式人生 > >呼叫免費的web service(天氣預報,手機號歸屬地)案例(四)

呼叫免費的web service(天氣預報,手機號歸屬地)案例(四)

1.Google免費WebService, 找到提供天氣預報Webservice的網路地址

將wsdl檔案儲存到本地(如果服務端使用net寫的,需修改wsdl)如下

      3. 建立java工程

4.客戶端編碼方式訪問

藉助命令工具自動生成客戶端程式碼

D:\work-space\webservice_04_jaxws_first_client\src>wsimport -keep -p C:\Users\Mr
.li\Desktop\test.wsdl

藉助生成的程式碼編寫請求程式碼

     5.編寫客戶端程式碼

public class testclient {
	public static void main(String[] args) {
		WeatherWS  WeatherWS=new WeatherWS();
		WeatherWSSoap   WeatherWSSoap=WeatherWS.getWeatherWSSoap();
		ArrayOfString  ArrayOfString=WeatherWSSoap.getWeather("上海", null);
		List<String> list=ArrayOfString.getString();
		for(String o:list){
			System.out.println(o);
			
		}
		
		MobileCodeWS MobileCodeWS=new MobileCodeWS();
		MobileCodeWSSoap MobileCodeWSSoap =MobileCodeWS.getMobileCodeWSSoap();
		String p=MobileCodeWSSoap.getMobileCodeInfo("15888888888", null);
		System.out.println(p);
	}
}

說明: 直接生成客戶端程式碼會拋異常, 無法生成客戶端程式碼, 解決辦法:

1.將對應的wsdl文件儲存到本地

2.修改wsdl文件的部分內容:

備註: 這個是Java呼叫net的webservice都有的問題