- /**
- * 一個利用Java獲取Google天氣預報的例子
- */
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.io.*;
- import java.net.MalformedURLException;
- import java.net.URL;
- import org.w3c.dom.*;
- import javax.swing.BorderFactory;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- import javax.xml.parsers.*;
- public class GoogleReport extends JFrame implements ActionListener {
- JPanel p1 = new JPanel();
- JTextField tf = new JTextField(2);
- JButton but = new JButton(“查詢”);
- JPanel p2 = new JPanel();
- JTextArea ta = new JTextArea(10, 40);
- public GoogleReport() {
- super(“天氣預報查詢”);
- // 設置p1面板
- p1.setLayout(new BorderLayout());
- tf.addKeyListener(new TfListener());// 設置tf監聽器
- tf.setBorder(BorderFactory.createLineBorder(Color.blue, 1));
- p1.add(tf, BorderLayout.CENTER);
- but.setBorder(BorderFactory.createLineBorder(Color.green));// 設置按鈕邊框顏色
- // 增加but事件監聽器
- but.addActionListener(this);
- p1.add(but, BorderLayout.EAST);
- add(p1, BorderLayout.NORTH);
- // 設置p2面板
- p2.setLayout(new GridLayout(1, 1));
- JScrollPane sp = new JScrollPane(ta);
- ta.setEnabled(false);
- ta.setBorder(BorderFactory.createLineBorder(Color.red, 2));
- p2.add(sp);
- add(p2, BorderLayout.CENTER);
- this.setSize(500, 400);
- this.setLocationRelativeTo(null);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- public static void main(String[] args) {
- new GoogleReport();
- }
- /**
- * getWeather->由city值獲得天氣情況
- *
- * @param city
- * @return 獲得的天氣情況的字符串
- */
- public String getWeather(String city) {
- try {
- URL ur = new URL(“https://www.google.com/ig/api?hl=zh_cn&weather=”
- city);
- InputStream instr = ur.openStream();
- String s;
- String str;
- BufferedReader in = new BufferedReader(new InputStreamReader(instr));
- StringBuffer sb = new StringBuffer();
- Writer out = new BufferedWriter(new OutputStreamWriter(
- new FileOutputStream(“weather.txt”), “utf-8”));
- while ((s = in.readLine()) != null) {
- sb.append(s);
- }
- str = new String(sb);
- out.write(str);
- out.close();
- in.close();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- File f = new File(“weather.txt”);
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- String str = null;
- try {
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document doc = builder.parse(f);
- NodeList nl = (NodeList) doc
- .getElementsByTagName(“forecast_conditions”);// 獲得XML文檔的forecast_conditions元素
- NodeList n2 = nl.item(0).getChildNodes();// 獲得第一個forecast_condition的全部子節點
- str = “今天是”
- n2.item(0).getAttributes().item(0).getNodeValue()
- “,”
- n2.item(4).getAttributes().item(0).getNodeValue()
- “,”// 獲得n2的第五個元素current_conditions
- &nbs