当前位置: 代码迷 >> 综合 >> 3.天气预报:编写客户端的主类
  详细解决方案

3.天气预报:编写客户端的主类

热度:18   发布时间:2024-01-17 03:28:16.0
package com.bjpowernode.ws;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
public class WeatherClient {
/**
* WSDL、服务端已经开发好,客户端静态调用服务端开放的服务(看到存根、骨架,因为是静态的) 
* 客户端要跟远端服务器打交道,实际上并不是直接打交道,而是通过远端服务器的代理,即是跟存根打交道,所以客户端 
* 必须得到存根 
* @param args
* @throws ServiceException 
* @throws RemoteException 
*/
public static void main(String[] args) throws ServiceException, RemoteException {
// 接口com.bjpowernode.ws.WeatherWebService可以通过getWeatherWebServiceSoap()得到存根,   
// 而类com.bjpowernode.ws.WeatherWebServiceLocator是对该接口的实现
WeatherWebService weatherWebService = new WeatherWebServiceLocator();
try {
WeatherWebServiceSoap_PortType weatherWebServiceSoap_PortType = weatherWebService.getWeatherWebServiceSoap();
System.out.println("本天气预报支持的省份如下:");
String[] provinces = weatherWebServiceSoap_PortType.getSupportProvince();
for (String province : provinces) {
System.out.println(province);
}
System.out.println("----------------------------------");
System.out.println("本天气预报支持的城市如下:");
String[] cities = weatherWebServiceSoap_PortType.getSupportCity("湖南");
for (String city : cities) {
System.out.println(city);
}
System.out.println("----------------------------------");
System.out.println("广州三天内的天气情况如下:");
String[] weatherForThreeDay = weatherWebServiceSoap_PortType.getWeatherbyCityName("深圳");
for (String weather : weatherForThreeDay) {
System.out.println(weather);
}
System.out.println("----------------------------------");
// 把截取到的异常往上抛
} catch (ServiceException e) {
e.printStackTrace();
throw e;
} catch (RemoteException e) {
e.printStackTrace();
throw e;
}
}
}