天气预报抓取天气信息模块实现(代码)

package com.zzk.cn;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.ws.ProtocolException;

public class CatchWeather {
	public static void main(String[] args ) {
		System.out.println("HelloWorld!");
		getInfo(101010100);
	}
	
	
	public static void getInfo(int i) {
		//String id="101010100";
		String path = "http://m.weather.com.cn/data/" + i + ".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) {
				System.out.println(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;
			
		}
		
	}
}

上一篇:马士兵J2SE-第三章-面向对象-Object类:toString方法、equals方法、对象转型、抽象类抽象方法、final关键字、接口


下一篇:庖丁解牛-系统基本指标对应分析工具图