调用中央气象台提供的开放接口简单实现获取天气信息

package WeatherInfo;

import java.net.URL;
import java.util.ArrayList;

import GeographicalLocation.GeoLocation;
import MyHttpClient.MyHttpClient;

public class CityCode {

	private String code_province;
	private String code_district;
	private String code_city;
	
	private String city;
	private String district;
	private String province;
	
	public  CityCode(String province,String district,String city){
		
		this.city = city;
		this.district = district;
		this.province = province;
		//System.out.println(province+" "+district+" "+city);
	}
	
	public String getCodeByURL(String location,String url){
		
		String code = null;
		try {

			MyHttpClient httpClient_weather = new MyHttpClient();
			String str = httpClient_weather.getContentByURL(url);
			//System.out.println(str);
			String[] strArray = str.split(",");
			for(int i = 0;i < strArray.length;i++){
				
				//System.out.println(strArray[i]);
				String[] strArr = strArray[i].split("\\|");
				if(location.indexOf(strArr[1]) != -1){
					
					code = strArr[0];
					//System.out.println(code);
				}
			}	
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return code;
	}
	
	public String getCodeByURL(String url){
		
		String code = null;
		try {

			MyHttpClient httpClient_weather = new MyHttpClient();
			String str = httpClient_weather.getContentByURL(url);
			//System.out.println(str);
			String[] strArray = str.split("\\|");
			code = strArray[1];
					//System.out.println(code)	
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return code;
	}
	
	public void getProvinceCode(){
		
		String url_province = "http://m.weather.com.cn/data5/city.xml";
		code_province = this.getCodeByURL(province, url_province);
		//System.out.println(code_province);
	}
	
	public void getDistrictCode(){
		try {
			
			URL url_district = new URL("http://m.weather.com.cn/data5/city"+code_province+".xml");
			String urlStr = url_district.toString();
			code_district = this.getCodeByURL(district,urlStr);
			//System.out.println(code_district);
		} catch (Exception e) {
			// TODO: handle exception
		}	
	}
	
	public void getCityCode(){
		
		try {
			this.getProvinceCode();
			this.getDistrictCode();
			URL url_city = new URL("http://m.weather.com.cn/data5/city"+code_district+".xml");
			String urlStr = url_city.toString();
			code_city = this.getCodeByURL(city,urlStr);
			//System.out.println(code_city);
		} catch (Exception e) {
			// TODO: handle exception
		}	
	}
	
	public String getWeatherCode(){
		
		String code_weather = null;
		getCityCode();
		try {
			URL url_weatherCode = new URL("http://m.weather.com.cn/data5/city"+code_city+".xml");
			String urlStr = url_weatherCode.toString();
			 code_weather = this.getCodeByURL(urlStr);
			//System.out.println(code_weather);
			return code_weather;
		} catch (Exception e) {
			return null;// TODO: handle exception
		}
	}
	
	public static void main(String[] args){
		
		GeoLocation myGeoLocation = new GeoLocation();
		ArrayList<String> location = myGeoLocation.getGeoLocation();
		
		//System.out.println(province+" "+district+" "+city);
		
		//String url = "http://m.weather.com.cn/data5/city.xml";
		
		CityCode cityCode = new CityCode(location.get(0),location.get(1),location.get(2));
		//cityCode.getCodeByURL(province, url);
		cityCode.getCityCode();
		String cityCode_weather = cityCode.getWeatherCode();
		String urlStr = null;
		try {
			
			URL url = new URL("http://m.weather.com.cn/data/"+cityCode_weather+".html");
			//URL url = new URL("http://m.weather.com.cn/data/"+101010100+".html");
			urlStr = url.toString();
			System.out.println(urlStr);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}

package WeatherInfo;

import java.net.URL;
import java.util.ArrayList;

import net.sf.json.JSONObject;
import GeographicalLocation.GeoLocation;
import MyHttpClient.MyHttpClient;

public class WeatherInfo {

	public String urlProducer(){
		
		String urlStr = null;
		GeoLocation myGeoLocation = new GeoLocation();
		ArrayList<String> mylocation = myGeoLocation.getGeoLocation();
		//System.out.println(province+" "+district+" "+city);
			
		CityCode my_CityCode = new CityCode(mylocation.get(0),mylocation.get(1),mylocation.get(2));
		//my_CityCode.getCityCode();
		String cityCode_weather = my_CityCode.getWeatherCode();
		try{
				
			URL url = new URL("http://m.weather.com.cn/data/"+cityCode_weather+".html");
			//URL url = new URL("http://m.weather.com.cn/data/"+101010100+".html");
			urlStr = url.toString();
			//System.out.println(urlStr);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return urlStr;
	}
	
	public void getWeatherInfo(){
		
		String urlStr = this.urlProducer();
		
		MyHttpClient myHttpClient = new MyHttpClient();
		String weatherInfo = myHttpClient.getContentByURL(urlStr);
		//System.out.println(weatherInfo);
		this.parseJSONData(weatherInfo);
	}
	
	private void parseJSONData(String weatherInfo){
		
		try {
			
			JSONObject data = JSONObject.fromObject(weatherInfo).getJSONObject("weatherinfo");
			//JSONObject data = obj.getJSONObject("weatherinfo");
			
			//The following data can be put in a ArrayList or a HashMap;s
			String city = data.getString("city");
			String date = data.getString("date_y");
			String week = data.getString("week");
			
			String temp = data.getString("temp1");
			String weather = data.getString("weather1");
			String wind = data.getString("wind1");
			//String fx = data.getString("fx1");
			String fl = data.getString("fl1");
			String index = data.getString("index");
			String index_d = data.getString("index_d");
			String index_uv = data.getString("index_uv");
			String index_co = data.getString("index_co");
			
			System.out.println("城市: "+city);
			System.out.println(date+"  "+week);
			System.out.println("气温: "+temp);
			System.out.println("天气: "+weather);
			System.out.println("风速: "+wind);
			System.out.println("风速级别: "+fl);
			System.out.println("穿衣指数: "+index+","+index_d);
			System.out.println("紫外线: "+index_uv);
			System.out.println("舒适指数: "+index_co);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public static void main(String[] args){
		
		WeatherInfo weatherInfo = new WeatherInfo();
		weatherInfo.getWeatherInfo();
	}
}


转载于:https://my.oschina.net/u/1184080/blog/190515

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值