2011-03-02 70 views
0

我想獲取網頁的源代碼。我使用HttpConnection 以下是我的代碼..如何從黑莓手機中的URL獲取HTML源代碼

HttpConnection c = null; 
     InputStream dis = null; 
     StringBuffer raw = new StringBuffer(); 
     try { 

       c = (HttpConnection)Connector.open(txtUrl.getText().toString()); 

      int len = 0; 
      int size = 0; 
      dis = c.openInputStream(); 
      byte[] data = new byte[256]; 
      while (-1 != (len = dis.read(data))){ 

       raw.append(new String(data, 0, len)); 
      size += len;  

      } 
      System.out.println("Html source"+raw.toString()); 

     } catch (IOException e) { 
       // TODO Auto-generated catch block 
      System.out.println("Exception " +e); 
      } 
     finally { 
      if (dis != null) 
       try { 
        dis.close(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        System.out.println("Exception " +e); 
       } 
      if (c != null) 
       try { 
        c.close(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        System.out.println("Exception " +e); 
       } 
     } 

在這條線dis = c.openInputStream();我得到的錯誤源未找到 - datagramProtocol(ConnectionBase).receive(數據報)。 如果我錯了請糾正我。

+0

也許你可以添加一些有關正在發生的事情的信息嗎?例如,「我期望看到'HTML source ...',但我真正看到的是'HTML source blah blah ...'」或「我在xyz行接收到NullPointerException」或類似內容。 – 2011-03-02 15:53:30

+0

我編輯了這個問題。請更正我..預先感謝 – user455422 2011-03-02 20:34:14

+0

未找到源不是錯誤,但調試器無法找到與拋出異常的代碼一起使用的源代碼。我們真的需要知道拋出了什麼異常,以及異常中包含哪些消息(如果有)。 – Richard 2011-03-03 04:19:09

回答