基于CXF框架访问远程webService服务之获取天气预报信息

本文主要讲述的是通过CXF框架实现的远程天气访问服务

WSDL:http://web.36wu.com/WeatherService.asmx?WSDL

开发首先通过wsdl2java命令访问远程webservice服务生成相关支持java代码(需要读者熟悉CXF框架)

 

实现类Test.java:

 

package client;

import java.util.ArrayList;
import java.util.List;

import com._36wu.ArrayOfWeatherDistrict;
import com._36wu.MoreWeather;
import com._36wu.ResultOfListOfWeatherDistrict;
import com._36wu.ResultOfMoreWeather;
import com._36wu.ResultOfTodayWeather;
import com._36wu.TodayWeather;
import com._36wu.WeatherDistrict;
import com._36wu.WeatherService;
import com._36wu.WeatherServiceSoap;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.demos.dto.Place;

public class Test {

	static List<Place> places = new ArrayList<Place>();
	static WeatherServiceSoap wss;
	//开发者验证号(个人)
	//有效期:24小时 
	//申请时间:2014-12-03
	//申请地址:http://www.36wu.com/Apply
	public static final String AUTHKEY = "42ceb3d44a54470c9348180972dd9e7e";
	
	static{
		WeatherService ws = new WeatherService();
		wss = ws.getWeatherServiceSoap();
		//根据提供的开发者id提供服务
		ResultOfListOfWeatherDistrict roowd = wss.getDistrictList("42ceb3d44a54470c9348180972dd9e7e");
		ArrayOfWeatherDistrict aowd = roowd.getData();
		List<WeatherDistrict> list = aowd.getWeatherDistrict();
		for (WeatherDistrict wd : list) {
			String p = wd.getProvince();//获得省
			String c = wd.getCity();//获得市
			String d = wd.getDistrict();//获得区
			int id = wd.getId();//获取id
			places.add(new Place(id, p, c, d));
		}
	}
	
	/**
	 * 根据输入的地区显示当前天气情况
	 * @param diqu
	 */
	public static void getToday(String diqu){
		ResultOfTodayWeather rtw = wss.getWeather(diqu, AUTHKEY);
		TodayWeather tw = rtw.getData();
		String city = tw.getCity();
		String imgs1 = tw.getImg1();
		String max = tw.getMaxTemp();
		String min = tw.getMinTemp();
		String humidity = tw.getHumidity();
		String weather = tw.getWeather();
		String temp = tw.getTemp();
		String winddir = tw.getWindDirection();
		String windforce = tw.getWindForce();

		p("城市:"+city);
		p("天气图片:"+imgs1);
		p("实时温度:"+temp);
		p("最低温度和最高温度:"+min+"~"+max);
		p("湿度:"+humidity);
		p("天气:"+weather);
		p("风向:"+winddir);
		p("风力:"+windforce);
	}
	
	/**
	 * 获取地区当天天气使用json返回
	 * @param diqu
	 * @return
	 */
	public static String getTodayOfJson(String diqu){
		ResultOfTodayWeather rtw = wss.getWeather(diqu, AUTHKEY);
		Gson gson = new GsonBuilder()
				.setPrettyPrinting()
				.setDateFormat("yyyy-MM-dd")
				.create();
		String json = gson.toJson(rtw);
		return json;
	}
	
	/**
	 * 获取未来一周天气以json字符串格式返回
	 * @param diqu
	 */
	public static String getMoreOfJson(String diqu){
		ResultOfMoreWeather romw = wss.getMoreWeather(diqu,AUTHKEY);
		Gson gson = new GsonBuilder()
						.setPrettyPrinting()
						.setDateFormat("yyyy-MM-dd")
						.create();
		String json = gson.toJson(romw);
		
		return json;
	}
	
	/**
	 * 根据输入的地区显示未来一周天气情况
	 * @param diqu
	 */
	public static void getMore(String diqu){
		ResultOfMoreWeather romw = wss.getMoreWeather(diqu, AUTHKEY);
		MoreWeather  tw = romw.getData();
		String city = tw.getCity();
		
		String imgs1 = tw.getImg1();	//天气图片
		String fl1 = tw.getFl1();		//第一天风力
		String date1 = tw.getDate1();	//第一天日期 /*注:只能获取第一天日期,后面剩余日期,读者可自行转换和计算,具体参考api:http://www.36wu.com/Service/Details/1*/
		String wind1 = tw.getWind1();	//第一天风向
		String temp1 = tw.getTemp1();	//第一天温度
		String weather1 = tw.getWeather1();	//第一天天气
		
		String imgs2 = tw.getImg2();	
		String fl2 = tw.getFl2();		
		String date2 = tw.getDate1();	
		String wind2 = tw.getWind2();	
		String temp2 = tw.getTemp2();	
		String weather2 = tw.getWeather2();	
		
		String imgs3 = tw.getImg3();	
		String fl3 = tw.getFl3();		
		String date3 = tw.getDate1();	
		String wind3 = tw.getWind3();	
		String temp3 = tw.getTemp3();	
		String weather3 = tw.getWeather3();	
		
		String imgs4 = tw.getImg4();	
		String fl4 = tw.getFl4();		
		String date4 = tw.getDate1();	
		String wind4 = tw.getWind4();	
		String temp4 = tw.getTemp4();	
		String weather4 = tw.getWeather4();	
		
		String imgs5 = tw.getImg5();	
		String fl5 = tw.getFl5();		
		String date5 = tw.getDate1();	
		String wind5 = tw.getWind5();	
		String temp5 = tw.getTemp5();	
		String weather5 = tw.getWeather5();	
		
		String imgs6 = tw.getImg6();	
		String fl6 = tw.getFl6();		
		String date6 = tw.getDate1();	
		String wind6 = tw.getWind6();	
		String temp6 = tw.getTemp6();	
		String weather6 = tw.getWeather6();	
		
		p("城市:"+city);
		p("=========第一天=========");
		p("日期:"+date1);
		p("天气图片:"+imgs1);
		p("温度:"+temp1);
		p("天气:"+weather1);
		p("风向:"+wind1);
		p("风力:"+fl1);
		
		p("=========第二天=========");
		p("日期:"+date2);
		p("天气图片:"+imgs2);
		p("温度:"+temp2);
		p("天气:"+weather2);
		p("风向:"+wind2);
		p("风力:"+fl2);
		
		p("=========第三天=========");
		p("日期:"+date3);
		p("天气图片:"+imgs3);
		p("温度:"+temp3);
		p("天气:"+weather3);
		p("风向:"+wind3);
		p("风力:"+fl3);
		
		p("=========第四天=========");
		p("日期:"+date4);
		p("天气图片:"+imgs4);
		p("温度:"+temp4);
		p("天气:"+weather4);
		p("风向:"+wind4);
		p("风力:"+fl4);
		
		p("=========第五天=========");
		p("日期:"+date5);
		p("天气图片:"+imgs5);
		p("温度:"+temp5);
		p("天气:"+weather5);
		p("风向:"+wind5);
		p("风力:"+fl5);
		
		p("=========第六天=========");
		p("日期:"+date6);
		p("天气图片:"+imgs6);
		p("温度:"+temp6);
		p("天气:"+weather6);
		p("风向:"+wind6);
		p("风力:"+fl6);
		p("---------------------");
		String indexd = tw.getIndexD();
		p(indexd);
	}
	
	public static void p(Object o){
		System.out.println(o);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//getToday("武汉");
		System.out.println(getTodayOfJson("武汉"));
		//String json = getMoreOfJson("江夏");
		//System.out.println(json);
		//getMore("武汉");
	}

}

上述代码主要功能:

 

getToday:获取当天天气情况

getTodayOfJson:获取当天天气情况以json格式返回

getMore:获取未来一周天气情况

getMoreOfJson:获取未来一周天气情况以json格式返回

另外该webService服务中包含获取全国所有省市区功能,因此封装了一个pojo类:Place.java

 

package com.demos.dto;

public class Place {

	private int id;				//地区id
	private String province;	//省
	private String city;		//市
	private String district;	//地区
	
	public Place() {
		// TODO Auto-generated constructor stub
	}
	
	public Place(int id, String province, String city, String district) {
		super();
		this.id = id;
		this.province = province;
		this.city = city;
		this.district = district;
	}

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getProvince() {
		return province;
	}
	public void setProvince(String province) {
		this.province = province;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getDistrict() {
		return district;
	}
	public void setDistrict(String district) {
		this.district = district;
	}
}

 

 

getToday方法调用结果如下:


 

getToday方法调用结果如下:

其他方法读者自行测试!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值