Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 28
Registered: ‎10-05-2009
My Device: Not Specified

Retrieve content from browserfield

Hey guys,

 

I am currently using a browserfield to send my user whizzing around the interweb.

Using this field I would like to retrieve the page content of their currently displayed webpage how can I extract this from the BrowserField control?

 

Thanks in advance

Developer
Posts: 410
Registered: ‎06-03-2010
My Device: Z10 Red
My Carrier: Free

Re: Retrieve content from browserfield

You may not I think. But you can intercept the data with your own BrowserFieldListener :

 

 actualBrowser.addListener(new MyBrowserFieldListener());

 

 

 public class MyBrowserFieldListener extends BrowserFieldListener {
  public void documentLoaded(BrowserField arg0, Document arg1) throws Exception {
//Collect the HTML data from arg1

 

}

}

Regular Contributor
Posts: 51
Registered: ‎01-13-2011
My Device: 9800
My Carrier: vodaphone

Re: Retrieve content from browserfield

How can icollect whole html from arg1 can you please give me code ..

Its urgent.

Thanx in advance

Vishal Lakhmapurkar
Contributor
Posts: 24
Registered: ‎06-03-2011
My Device: Curve
My Carrier: Vodacom SA

Re: Retrieve content from browserfield

I have tried this - but the listener never seems to get called.

 

code extract:

 

browserField=new BrowserField();

 

 listen=new Mybrowserlistener();
 browserField.addListener(listen);


 // add the browser field to a ui manager or screen
 screen.add( browserField );

 // request the content you wish to display

 

 browserField.requestContent(browser_url);
 
 log_tx=new String("\nThe html:\n"+listen.get_text());
 
         tx=new LabelField(log_tx);
         screen.add(tx);

 

It shows the page - but does not display the  actual html

 

The listener class:

 

public class Mybrowserlistener extends BrowserFieldListener {
   
    String raw_html;
    Mybrowserlistener() {   
    raw_html=new String("..");
    }
 

  public void documentLoaded(BrowserField arg0, Document arg1) {
//Collect the HTML data from arg1

raw_html=new String(arg1.getTextContent());

}

public String get_text(){
  return raw_html;  
}
}

 

 

Contributor
Posts: 24
Registered: ‎06-03-2011
My Device: Curve
My Carrier: Vodacom SA

Re: Retrieve content from browserfield

I think the problem is the implementation of the listener (in example above).

That code simply never gets called.  Any idea how to implement it ?

 

 

 

Regular Contributor
Posts: 51
Registered: ‎01-13-2011
My Device: 9800
My Carrier: vodaphone

Re: Retrieve content from browserfield

Ya i also facing the same problem :-(

Vishal Lakhmapurkar
Contributor
Posts: 24
Registered: ‎06-03-2011
My Device: Curve
My Carrier: Vodacom SA

Re: Retrieve content from browserfield

 

I have retried it  with the JDK documentation example  the listener is not getting called. The code below should cause the screen to display at least one instance of  'listener called'  - it simply does not !

 

 String tx_log=new String("html code:");

 browserField = new BrowserField();


 BrowserFieldListener listener = new BrowserFieldListener() {
     public void documentLoaded(BrowserField browserField, Document document) throws Exception
     {

       log_tx=log_tx.concat("listener called");
//       log_tx=new String(document.getTextContent());

         // the document has loaded, do something ...
     }
};

 browserField.addListener( listener );

 // add the browser field to a ui manager or screen
 screen.add( browserField );

 // request the content you wish to display
 // this method call is typically called once
 browserField.requestContent(browser_url);
 
// log_tx=new String("\nThe html:\n"+listen.get_text());
 
         tx=new LabelField(log_tx);
         screen.add(tx);

 }

 

Regular Contributor
Posts: 51
Registered: ‎01-13-2011
My Device: 9800
My Carrier: vodaphone

Re: Retrieve content from browserfield

i am getting null value

System.out.println(document.getTextContebt());

Vishal Lakhmapurkar
Contributor
Posts: 24
Registered: ‎06-03-2011
My Device: Curve
My Carrier: Vodacom SA

Re: Retrieve content from browserfield

Yes - you are getting null because the listener is never called.  Does anyone know how to implement the listener ?

 

Developer
Posts: 28
Registered: ‎10-05-2009
My Device: Not Specified

Re: Retrieve content from browserfield

In the above example are you just after the page content then I would do this differently and not use browserField at all and do a httpGet class to beam the content to your code its alot quicker thna invoking the whole browserfield.

I never actually got this to work properly instead I cheated and used the getBaseUrl and did everything from their dirty solution but it worked for me.