2025-02-17

  1. /**
  2.  * 一個利用Java獲取Google天氣預報的例子
  3.  */
  4. import java.awt.BorderLayout;
  5. import java.awt.Color;
  6. import java.awt.GridLayout;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.KeyAdapter;
  10. import java.awt.event.KeyEvent;
  11. import java.io.*;
  12. import java.net.MalformedURLException;
  13. import java.net.URL;
  14. import org.w3c.dom.*;
  15. import javax.swing.BorderFactory;
  16. import javax.swing.JButton;
  17. import javax.swing.JFrame;
  18. import javax.swing.JPanel;
  19. import javax.swing.JScrollPane;
  20. import javax.swing.JTextArea;
  21. import javax.swing.JTextField;
  22. import javax.xml.parsers.*;
  23. public class GoogleReport extends JFrame implements ActionListener {
  24.     JPanel p1 = new JPanel();
  25.     JTextField tf = new JTextField(2);
  26.     JButton but = new JButton(“查詢”);
  27.     JPanel p2 = new JPanel();
  28.     JTextArea ta = new JTextArea(10, 40);
  29.     public GoogleReport() {
  30.         super(“天氣預報查詢”);
  31.         // 設置p1面板
  32.         p1.setLayout(new BorderLayout());
  33.         tf.addKeyListener(new TfListener());// 設置tf監聽器
  34.         tf.setBorder(BorderFactory.createLineBorder(Color.blue, 1));
  35.        
  36.         p1.add(tf, BorderLayout.CENTER);
  37.         but.setBorder(BorderFactory.createLineBorder(Color.green));// 設置按鈕邊框顏色
  38.         // 增加but事件監聽器
  39.         but.addActionListener(this);
  40.         p1.add(but, BorderLayout.EAST);
  41.         add(p1, BorderLayout.NORTH);
  42.         // 設置p2面板
  43.         p2.setLayout(new GridLayout(1, 1));
  44.         JScrollPane sp = new JScrollPane(ta);
  45.         ta.setEnabled(false);
  46.         ta.setBorder(BorderFactory.createLineBorder(Color.red, 2));
  47.         p2.add(sp);
  48.         add(p2, BorderLayout.CENTER);
  49.         this.setSize(500, 400);
  50.         this.setLocationRelativeTo(null);
  51.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  52.         this.setVisible(true);
  53.     }
  54.     public static void main(String[] args) {
  55.         new GoogleReport();
  56.     }
  57.     /**
  58.      * getWeather->由city值獲得天氣情況
  59.      *
  60.      * @param city
  61.      * @return 獲得的天氣情況的字符串
  62.      */
  63.     public String getWeather(String city) {
  64.         try {
  65.             URL ur = new URL(“https://www.google.com/ig/api?hl=zh_cn&weather=”
  66.                     city);
  67.             InputStream instr = ur.openStream();
  68.             String s;
  69.             String str;
  70.             BufferedReader in = new BufferedReader(new InputStreamReader(instr));
  71.             StringBuffer sb = new StringBuffer();
  72.             Writer out = new BufferedWriter(new OutputStreamWriter(
  73.                     new FileOutputStream(“weather.txt”), “utf-8”));
  74.             while ((s = in.readLine()) != null) {
  75.                 sb.append(s);
  76.             }
  77.             str = new String(sb);
  78.             out.write(str);
  79.             out.close();
  80.             in.close();
  81.         } catch (MalformedURLException e) {
  82.             e.printStackTrace();
  83.         } catch (IOException e) {
  84.             e.printStackTrace();
  85.         }
  86.         File f = new File(“weather.txt”);
  87.         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  88.         String str = null;
  89.         try {
  90.             DocumentBuilder builder = factory.newDocumentBuilder();
  91.             Document doc = builder.parse(f);
  92.             NodeList nl = (NodeList) doc
  93.                     .getElementsByTagName(“forecast_conditions”);// 獲得XML文檔的forecast_conditions元素
  94.             NodeList n2 = nl.item(0).getChildNodes();// 獲得第一個forecast_condition的全部子節點
  95.             str = “今天是”
  96.                     n2.item(0).getAttributes().item(0).getNodeValue()
  97.                     “,”
  98.                     n2.item(4).getAttributes().item(0).getNodeValue()
  99.                     “,”// 獲得n2的第五個元素current_conditions
  100.       &nbs

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *