天気予報キャプチャ天気情報モジュール実装(コード)


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;
			
		}
		
	}
}