天气预报抓取、解析(传递抓取的info参数)功能模块实现(代码)

package com.zzk.cn;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;

import net.sf.json.JSONObject;

/**
 *
 * @author zhuzhengke
 * @version 1.0.0
 * 
 */


public class Json {

	static String info = "";

	/*主函数*/
	public static void main(String[] args) throws UnsupportedEncodingException {
		//抓取网页
		getInfo(101010100);
		//解析字段
		readJson();
	}
	
	/**
	 * 获取网页信息
	 */
	public static void getInfo(int id) {
		
		String path = "http://m.weather.com.cn/data/"+id+".html";
		URL url;
		String inputline = "";
		InputStream input = null;
		InputStreamReader reader = null;
		BufferedReader buffer = null;
		
		try {
			url = new URL(path);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setReadTimeout(10 * 1000);
			conn.setRequestMethod("GET");

			input = conn.getInputStream();
			reader = new InputStreamReader(input,"utf8");
			buffer = new BufferedReader(reader);

			while ((inputline = buffer.readLine()) != null) {
				info += inputline;
			}

		} catch (ProtocolException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			//关闭资源
			try {
				if (null != buffer) {
					buffer.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			buffer = null;
			
			try {
				if (null != reader) {
					reader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			reader = null;
			
			try {
				if (null != input) {
					input.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			input = null;
		}
	}
	
	/**
	 * Json解析
	 */
	public static void readJson() {

		
		
		JSONObject jsonob = JSONObject.fromObject((JSONObject.fromObject(info)
				.getString("weatherinfo")));

		String city = jsonob.getString("city");// 城市
		System.out.println(city);

		String date_y = jsonob.getString("date_y");// 时间
		System.out.println(date_y);

		String date = jsonob.getString("date");// 农历年
		System.out.println(date);

		String week = jsonob.getString("week");// 星期
		System.out.println(week);

		String cityid = jsonob.getString("cityid");// 城市号
		System.out.println(cityid);

		String temp1 = jsonob.getString("temp1");// 以下是六天内摄氏温度
		System.out.println(temp1);

		String temp2 = jsonob.getString("temp2");
		System.out.println(temp2);

		String temp3 = jsonob.getString("temp3");
		System.out.println(temp3);

		String temp4 = jsonob.getString("temp4");
		System.out.println(temp4);

		String temp5 = jsonob.getString("temp5");
		System.out.println(temp5);

		String temp6 = jsonob.getString("temp6");
		System.out.println(temp6);

		String tempF1 = jsonob.getString("tempF1");// 以下是六天内华氏温度
		System.out.println(tempF1);

		String tempF2 = jsonob.getString("tempF2");
		System.out.println(tempF2);

		String tempF3 = jsonob.getString("tempF3");
		System.out.println(tempF3);

		String tempF4 = jsonob.getString("tempF4");
		System.out.println(tempF4);

		String tempF5 = jsonob.getString("tempF5");
		System.out.println(tempF5);

		String tempF6 = jsonob.getString("tempF6");
		System.out.println(tempF6);

		String weather1 = jsonob.getString("weather1");// 以下是六天天气
		System.out.println(weather1);

		String weather2 = jsonob.getString("weather2");
		System.out.println(weather2);

		String weather3 = jsonob.getString("weather3");
		System.out.println(weather3);

		String weather4 = jsonob.getString("weather4");
		System.out.println(weather4);

		String weather5 = jsonob.getString("weather5");
		System.out.println(weather5);

		String weather6 = jsonob.getString("weather6");
		System.out.println(weather6);

		String wind1 = jsonob.getString("wind1");// 以下六天为风力
		System.out.println(wind1);

		String wind2 = jsonob.getString("wind2");
		System.out.println(wind2);

		String wind3 = jsonob.getString("wind3");
		System.out.println(wind3);

		String wind4 = jsonob.getString("wind4");
		System.out.println(wind4);

		String wind5 = jsonob.getString("wind5");
		System.out.println(wind5);

		String wind6 = jsonob.getString("wind6");
		System.out.println(wind6);

		String fl1 = jsonob.getString("fl1");// 以下为六天风级
		System.out.println(fl1);

		String fl2 = jsonob.getString("fl2");
		System.out.println(fl2);

		String fl3 = jsonob.getString("fl3");
		System.out.println(fl3);

		String fl4 = jsonob.getString("fl4");
		System.out.println(fl4);

		String fl5 = jsonob.getString("fl5");
		System.out.println(fl5);

		String fl6 = jsonob.getString("fl6");
		System.out.println(fl6);

		String index_d = jsonob.getString("index_d");// 当日穿衣指数
		System.out.println(index_d);
	}

	
}


 

上一篇:马士兵J2SE-第三章-面向对象-static、继承、重写、构造函数


下一篇:马士兵J2SE-第九章-多线程机制-举例、线程同步之锁