简单的天气调用

工作需要 展示一个简单的天气 在此做下记录 方便以后看下
package com.tempus.cqs.common.util.entity;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.jettison.json.JSONObject;
import org.codehaus.jettison.json.JSONTokener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WeatherJsonUtil {
	private static Logger logger = LoggerFactory.getLogger(Weather.class);

	// 中央气象台的服务
	public static Map<String, Object> getWeathMapFirst() {
		Map<String, Object> weathMap = null;
		try {
			String url = "http://www.nmc.gov.cn/publish/weather/24/59493.html";
			URL weathUrl = new URL(url);
			HttpURLConnection connection = (HttpURLConnection) weathUrl
					.openConnection();
			InputStream in = connection.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					in, "utf-8"));
			String result = reader.readLine();

			JSONTokener jt = new JSONTokener(result);
			JSONObject resultObject = (JSONObject) jt.nextValue();
			

			Weather weather1 = new Weather();
			weather1.setCity("深圳");
			weather1.setDate(resultObject.getString("day1"));
			weather1.setImgName("http://forcast.weathertv.cn/img/weather_icon/"+dealImgNameFirst(resultObject.getString("img1"))+".png");
			weather1.setTemp(resultObject.getString("temp1"));
			weather1.setWeatherInfor(resultObject.getString("weather1"));
			weather1.setWind(resultObject.getString("wind1"));

			Weather weather2 = new Weather();
			weather2.setCity("深圳");
			weather2.setDate(resultObject.getString("day2"));
			weather2.setImgName("http://forcast.weathertv.cn/img/weather_icon/"+dealImgNameFirst(resultObject.getString("img2"))+".png");
			weather2.setTemp(resultObject.getString("temp2"));
			weather2.setWeatherInfor(resultObject.getString("weather2"));
			weather2.setWind(resultObject.getString("wind2"));

			weathMap = new HashMap<String, Object>();
			weathMap.put("dayWeath", weather1);
			weathMap.put("nightWeath", weather2);
			
			in.close();
			reader.close();

		} catch (Exception e) {
			logger.error("解析中央气象台错误,参考地址http://www.nmc.gov.cn/");
		}

		return weathMap;
	}
	//中央气象网的服务 之前可以连接 现在却不能解析到了
	public static Map<String, Object> getWeatherSecond() {
		Map<String, Object> weathMap = null;
		try {
			String url = "http://m.weather.com.cn/atad/101280601.html";
			URL weathUrl = new URL(url);
			HttpURLConnection connection = (HttpURLConnection) weathUrl
					.openConnection();
			InputStream in = connection.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					in, "utf-8"));
			String result = reader.readLine();
		

			JSONTokener jt = new JSONTokener(result);
			JSONObject jn;
			jn = (JSONObject) jt.nextValue();
			String weatherinfo = jn.getString("weatherinfo");

			JSONTokener resutltTokener = new JSONTokener(weatherinfo);
			JSONObject resultObject = (JSONObject) resutltTokener.nextValue();

			Weather weather1 = new Weather();
			weather1.setDate(resultObject.getString("day1"));
			weather1.setImgName(resultObject.getString("img1"));
			weather1.setTemp(resultObject.getString("temp1"));
			weather1.setWeatherInfor(resultObject.getString("weather1"));
			weather1.setWind(resultObject.getString("wind1"));

			Weather weather2 = new Weather();
			weather2.setDate(resultObject.getString("day2"));
			weather2.setImgName(resultObject.getString("img2"));
			weather2.setTemp(resultObject.getString("temp2"));
			weather2.setWeatherInfor(resultObject.getString("weather2"));
			weather2.setWind(resultObject.getString("wind2"));

			Weather weather3 = new Weather();
			weather3.setDate(resultObject.getString("day3"));
			weather3.setImgName(resultObject.getString("img3"));
			weather3.setTemp(resultObject.getString("temp3"));
			weather3.setWeatherInfor(resultObject.getString("weather3"));
			weather3.setWind(resultObject.getString("wind3"));
			
			weathMap = new HashMap<String, Object>();
			weathMap.put("day1", weather1);
			weathMap.put("day2", weather2);
			weathMap.put("day3", weather3);
			in.close();
			reader.close();
		} catch (Exception e) {
			logger.error("解析中央气象网错误,参考地址( http://www.cma.gov.cn/ )");
		}
		return weathMap;
	}
	
	//处理图片名字的方法 由于d01--d1这种变化
	public static String dealImgName(String imgName){
		if(imgName.contains("d0")){
			imgName = imgName.substring(imgName.indexOf("0")+1, imgName.length());
			imgName = "d" + imgName;
		}
		if(imgName.contains("n0")){
			imgName = imgName.substring(imgName.indexOf("0")+1, imgName.length());
			imgName = "n" + imgName;
		}
		return imgName;
	}
	//处理图片名字的方法 由于d01--01这种变化
	public static String dealImgNameFirst(String imgName){
		if(imgName.startsWith("d")){
			imgName = imgName.substring(1,imgName.length());
		}
		if(imgName.startsWith("n")){
			imgName = imgName.substring(1,imgName.length());
		}
		return imgName;
	} 
}

你可能感兴趣的:(调用)