如何从黑莓手机中获取HTML源代码

时间:2011-03-02 15:42:51

标签: blackberry

我想获取网页的源代码。我正在使用HttpConnection 以下是我的代码..

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(Datagram)。 我出错的地方请纠正我..

1 个答案:

答案 0 :(得分:2)

您应该阅读javax.microedition.io.HttpConnection的API文档,并按照建议执行输入例程。