JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java SE 討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Re:URLConnection 的 getOutputStream() and getInputStream() [Re:weaker]
weaker

小屁屁∼



發文: 379
積分: 4
於 2004-02-13 18:08 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我現在知道問題所在了
重點就是
Duncan所說的

Server site 在你送出參數將結果送回來後就不再服務/對談了(這個 connection 也就沒用了)。你可以每次要送出查詢的單字建立一個 connection。

而 liminjun 大說的其實並沒有問題
問題是我忘記把 start 重新設回 false;
因此在第二次查詢時,一開始就 break 掉了
所以當然沒有讀取到任何資料
一點小錯誤害我想好久 :~
以下為正確的 code
謝謝各位的幫忙
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.regex.*;
 
public class Dict implements ActionListener {
  
  final static String address = "http://afl.chna.edu.tw/dict/dict2.php";
  Pattern p1 = Pattern.compile("<(/)?pre>",Pattern.CASE_INSENSITIVE);
  Pattern p2 = Pattern.compile("<font color=#\\S+>",Pattern.CASE_INSENSITIVE);
  Matcher m1 = p1.matcher("");
  Matcher m2 = p2.matcher("");
  boolean start = false;
  private URL url = null;
  private URLConnection conn = null;
  private BufferedWriter bw = null;
  private BufferedReader br = null;
  private StringBuffer sb = new StringBuffer();
  private JButton send = null;
  private JButton clear = null;
  private JTextField inputField = null;
  private JTextArea result = null;
  private boolean test = false;
 
  public Dict() {
    initialize();
    JFrame f = new JFrame("my Dict");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container c = f.getContentPane();
    c.setLayout(new GridBagLayout());
    GridBagConstraints cons = new GridBagConstraints();
    cons.gridx = 0; cons.gridy = 0;
    cons.fill = GridBagConstraints.BOTH;
    c.add(getResultPanel(), cons);
    cons.gridy = 1;
    c.add(getInputField(), cons);
    cons.gridy = 2;
    c.add(getButtonPanel(), cons);
    f.pack();
    f.show();
  }
  
  public void initialize() {
    try {
      url = new URL(address);
      conn = url.openConnection();
      conn.setDoInput(true);
      conn.setDoOutput(true);
      bw = new BufferedWriter(
          new OutputStreamWriter(
              conn.getOutputStream(), "8859_1"));          
    } catch (Exception e) {
      e.printStackTrace();
    }    
  }
  
  public JPanel getResultPanel() {
    JPanel resultField = new JPanel();
    result = new JTextArea(15, 40);
    JScrollPane scrollPane = new JScrollPane(result);
    resultField.add(scrollPane);
    return resultField;
  }
  
  public JPanel getInputField() {
    JPanel inputFieldPanel = new JPanel();
    inputField = new JTextField(15);
    inputFieldPanel.add(inputField);
    return inputFieldPanel;
  }
  
  public JPanel getButtonPanel() {
    JPanel buttonPanel = new JPanel();
    send = new JButton("send");
    send.addActionListener(this);
    clear = new JButton("clear");
    clear.addActionListener(this);
    buttonPanel.add(send);
    buttonPanel.add(clear);
    return buttonPanel; 
  }
  
  public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("send")) {
       initialize();
       String str = "keyword=" + inputField.getText() + "&dict=Dictionary2\r\n";
       try {
         bw.write(str);
         bw.flush();
         br = new BufferedReader(
             new InputStreamReader(
                 conn.getInputStream()));
         String tmp = "";
         while ((tmp = br.readLine()) != null) {
           m1.reset(tmp);
           if (m1.find()) {
             if (start)
               break;
             else 
               start = true;continue;
           }
           if (start) {
             m2.reset(tmp);
             if (m2.find()) {
               tmp = tmp.replaceAll("<(/)?font(\\s\\S+)?>", "");
             }
             sb.append(tmp + "\r\n");
           }
         }
         result.setText(sb.toString());
         start = false;
         sb.delete(0, sb.toString().length());
       } catch (Exception ee) {
         ee.printStackTrace();
       } finally {
         try {
           if (bw != null) bw.close();
           if (br != null) br.close();
         } catch (Exception ignored) {}
       }
    } else if (e.getActionCommand().equals("clear")) {
      inputField.setText("");
      result.setText("");
    }
    
  }
  
  public static void main(String[] args) {
    new Dict();
  }
}


Dict.java (3.51k)


reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
6147 URLConnection 的 getOutputStream() and getInputStream() weaker 3570 2004-02-13 14:34
4190 Re:URLConnection 的 getOutputStream() and getInputStream() Duncan 242 2004-02-13 16:38
7142 Re:URLConnection 的 getOutputStream() and getInputStream() Yoshi 2447 2004-02-14 07:36
3717 Re:URLConnection 的 getOutputStream() and getInputStream() smallsheep 377 2004-02-19 16:34
3744 Re:URLConnection 的 getOutputStream() and getInputStream() Yoshi 533 2004-02-19 17:10
3947 Re:URLConnection 的 getOutputStream() and getInputStream() smallsheep 607 2004-02-21 18:51
3736 Re:URLConnection 的 getOutputStream() and getInputStream() Yoshi 979 2004-02-21 23:11
3619 Re:URLConnection 的 getOutputStream() and getInputStream() Yoshi 575 2004-02-22 08:50
3850 Re:URLConnection 的 getOutputStream() and getInputStream() dennylai 717 2004-02-23 09:45
4002 Re:URLConnection 的 getOutputStream() and getInputStream() liminjun 6032 2004-02-13 16:58
3624 Re:URLConnection 的 getOutputStream() and getInputStream() weaker 6151 2004-02-13 17:33
3559 Re:URLConnection 的 getOutputStream() and getInputStream() liminjun 266 2004-02-13 17:40
4433 Re:URLConnection 的 getOutputStream() and getInputStream() weaker 7529 2004-02-13 18:08
3493 Re:URLConnection 的 getOutputStream() and getInputStream() liminjun 362 2004-02-13 18:18
3482 Re:URLConnection 的 getOutputStream() and getInputStream() Yoshi 482 2004-02-14 07:47
3487 Re:URLConnection 的 getOutputStream() and getInputStream() weaker 482 2004-02-14 15:28
3469 Re:URLConnection 的 getOutputStream() and getInputStream() Duncan 738 2004-02-14 15:54
3684 Re:URLConnection 的 getOutputStream() and getInputStream() weaker 4929 2004-02-18 11:08
» JWorld@TW »  Java SE 討論區

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8