2011-03-02 71 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

回答