user455422
user455422

Reputation: 47

How to get HTML source code from an url in blackberry

I want to get the source code of a webpage. I am using HttpConnection following is my code..

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);
                }
         }

At this line dis = c.openInputStream(); i am getting error as source not found - datagramProtocol(ConnectionBase).receive(Datagram). Where I am going wrong please correct me..

Upvotes: 0

Views: 715

Answers (1)

Richard
Richard

Reputation: 8920

You should probably read the API documentation for javax.microedition.io.HttpConnection and implement your input routine as recommended there.

Upvotes: 2

Related Questions