獲取Google的簡單天氣預報(ZZ)

          注意weather那寫入城市的拼音轉化一下就行打開之后是XML格式的然后再提取。


          1 package com.pmjava.util;
          2
          3 import java.io.BufferedReader;
          4 import java.io.BufferedWriter;
          5 import java.io.FileWriter;
          6 import java.io.IOException;
          7 import java.io.InputStream;
          8 import java.io.InputStreamReader;
          9 import java.net.MalformedURLException;
          10 import java.net.URL;
          11
          12
          13 import java.io.*;
          14 import org.w3c.dom.*;
          15 import javax.xml.parsers.*;
          16
          17 public class GetWeather {
          18    
          19    
          20     public String getweather(String city)
          21     {
          22         try {
          23             URL ur = new URL("http://www.google.com/ig/api?hl=zh_cn&weather="+city);
          24             InputStream instr = ur.openStream();
          25             String s, str;
          26             BufferedReader in = new BufferedReader(new InputStreamReader(instr));
          27             StringBuffer sb = new StringBuffer();
          28            
          29 Writer   out   =   new   BufferedWriter(new OutputStreamWriter(new   FileOutputStream("weather.txt"),   "utf-8"));
          30             while ((s = in.readLine()) != null) {
          31                 sb.append(s);
          32             }
          33             str = new String(sb);
          34             out.write(str);
          35             out.close();
          36             in.close();
          37                
          38
          39         } catch (MalformedURLException e) {
          40             e.printStackTrace();
          41         } catch (IOException e) {
          42             e.printStackTrace();
          43         }
          44         File f=new File("weather.txt");
          45         DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
          46         String str=null;
          47         try{
          48         DocumentBuilder builder=factory.newDocumentBuilder();
          49         Document doc = builder.parse(f);
          50         NodeList nl =  (NodeList) doc.getElementsByTagName("forecast_conditions");
          51         NodeList n2=nl.item(0).getChildNodes();
          52        
          53 str=n2.item(4).getAttributes().item(0).getNodue()+","+n2.item(1).getAttributes().item(0).getNodue()+"℃-"+n2.item(2).getAttributes().item(0).getNodue()+"℃";
          54         }catch(Exception e)
          55         {
          56            
          57         }
          58        
          59         return str;
          60     }
          61    
          62        
          63    
          64 }

          posted on 2009-07-15 13:35 想飛就飛 閱讀(157) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          公告


          導航

          <2009年7月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          統計

          常用鏈接

          留言簿(13)

          我參與的團隊

          隨筆分類(69)

          隨筆檔案(68)

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 深水埗区| 海晏县| 开江县| 江川县| 崇州市| 平陆县| 东源县| 富民县| 彝良县| 天祝| 满洲里市| 巨野县| 枝江市| 瑞昌市| 定远县| 鄂尔多斯市| 汕头市| 东阳市| 娄烦县| 荔波县| 谷城县| 科尔| 海南省| 天等县| 准格尔旗| 嵩明县| 临汾市| 南漳县| 府谷县| 同江市| 乐至县| 元朗区| 东至县| 汝城县| 贺州市| 攀枝花市| 红原县| 永昌县| 大港区| 怀仁县| 社会|