天气预报抓取天气信息模块实现(代码)
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 ; String path = "https://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; } } }
最后更新:2017-04-02 06:52:11