Sorry about the red box, but we really need you to update your browser. Read this excellent article if you're wondering why we are no longer supporting this browser version. Go to Browse Happy for browser suggestions and how to update.

BlackBerry Browser

Retrieve a BlackBerry Browser session

Retrieving the default session overrides any open sessions on the BlackBerry device.

Import the net.rim.blackberry.api.browser.Browser class.

Retrieve the default BrowserSession object by invoking the static method Browser.getDefaultSession().

Retrieve a non-default BlackBerry Browser session

Import the net.rim.blackberry.api.browser.Browser class.

Invoke Browser.getSession(uid), where uid is the browser's service record UID.

Request a web page

Import the net.rim.blackberry.api.browser.BrowserSession class.

Invoke BrowserSession.displayPage(String url), specifying the URL that contains the web content.

The following sample creates a menu item that displays a web page in the BlackBerry Browser.

private MenuItem browserItem = 
   new MenuItem(_resources.getString(MENUITEM_BROWSER), 110, 12) 
{
   public void run() 
   {
      BrowserSession visit = Browser.getDefaultSession();
      visit.displayPage("http://www.blackberry.com");
   }
};

Enhanced support for web content in BlackBerry device applications

In BlackBerry Java Development Environment 5.0, the net.rim.device.api.browser.field2 package provides a new set of APIs you can use to embed web content in BlackBerry device applications.

In earlier versions of the BlackBerry JDE, the net.rim.device.api.browser.field package provided functionality to add web content to a BlackBerry device application. For more information about using this browser field, see the BrowserFieldDemo sample application that is provided with the BlackBerry JDE.

The APIs that are new in BlackBerry JDE 5.0 allow you to perform the following actions:

  • load and configure the display settings for web content in Field objects in any application
  • access the DOM for loaded web content
  • control the HTTP handling for connections, SSL, cookies, and caching
  • set callback listeners to monitor the loading progress of web pages

You can create applications that include Java objects and methods that can access and invoke JavaScript code and be accessed and invoked by JavaScript code by using the APIs in the net.rim.device.api.script package, which is new in BlackBerry JDE 5.0.

Display HTML content in a browser field

Import the required classes and interfaces.

import net.rim.device.api.browser.field2.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;

Create the application framework by extending the UiApplication class. In main(), create an instance of the new class and invoke enterEventDispatcher() to enable the application to receive events. In the application constructor, invoke pushScreen() to display the custom screen for the application. The BrowserFieldDemoScreen class represents the custom screen.

public class BrowserFieldDemo extends UiApplication
{
     public static void main(String[] args)
     {
          BrowserFieldDemo app = new BrowserFieldDemo();
          app.enterEventDispatcher();
     }

     public BrowserFieldDemo() 
     {
          pushScreen(new BrowserFieldDemoScreen());
     }
}

Create the custom screen by extending the MainScreen class.

class BrowserFieldDemoScreen extends MainScreen
{
     public BrowserFieldDemoScreen()
     {
     }
}

In the screen constructor, create an instance of the BrowserField class.

BrowserField myBrowserField = new BrowserField();

In the screen constructor, invoke add() to add the BrowserField object to the screen.

add(myBrowserField);

In the screen constructor, invoke BrowserField.displayContent() to specify and display the HTML content.

myBrowserField.displayContent("<html><body><h1>Hello World!</h1></body></html>", "http://localhost");

Display HTML content from a web page in a browser field

Import the required classes and interfaces.

import net.rim.device.api.browser.field2.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;

Create the application framework by extending the UiApplication class. In main(), create an instance of the new class and invoke enterEventDispatcher() to enable the application to receive events. In the application constructor, invoke pushScreen() to display the custom screen for the application. The BrowserFieldDemoScreen class represents the custom screen.

public class BrowserFieldDemo extends UiApplication
{
     public static void main(String[] args)
     {
          BrowserFieldDemo app = new BrowserFieldDemo();
          app.enterEventDispatcher();
     }

     public BrowserFieldDemo() 
     {
          pushScreen(new BrowserFieldDemoScreen());
     }
}

Create the framework for the custom screen by extending the MainScreen class.

class BrowserFieldDemoScreen extends MainScreen
{
     public BrowserFieldDemoScreen()
     {
     }
}

In the screen constructor, create an instance of the BrowserField class.

BrowserField myBrowserField = new BrowserField();

In the screen constructor, invoke add() to add the BrowserField object to the screen.

add(myBrowserField);

In the screen constructor, invoke BrowserField.requestContent() to specify the location of the HTML content and display it.

myBrowserField.requestContent("http://www.blackberry.com");

Display HTML content from a resource in your application

Import the required classes and interfaces.

import net.rim.device.api.browser.field2.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;

Create the application framework by extending the UiApplication class. In main(), create an instance of the new class and invoke enterEventDispatcher() to enable the application to receive events. In the application constructor, invoke pushScreen() to display the custom screen for the application. The BrowserFieldDemoScreen class represents the custom screen.

public class BrowserFieldDemo extends UiApplication
{
     public static void main(String[] args)
     {
          BrowserFieldDemo app = new BrowserFieldDemo();
          app.enterEventDispatcher();
     }

     public BrowserFieldDemo() 
     {
          pushScreen(new BrowserFieldDemoScreen());
     }
}

Create the custom screen by extending the MainScreen class.

class BrowserFieldDemoScreen extends MainScreen
{
     public BrowserFieldDemoScreen()
     {
     }
}

In the screen constructor, create an instance of the BrowserField class.

BrowserField myBrowserField = new BrowserField();

In the screen constructor, invoke add() to add the BrowserField object to the screen.

add(myBrowserField);

In the screen constructor, invoke BrowserField.requestContent() to specify the location of the resource in your application and display the HTML content.

myBrowserField.requestContent("local:///test.html");

The BrowserField class does not access resources using a folder structure. The BrowserField class displays the first resource found that matches the specifed file name.

Configure a browser field

Import the required classes and interfaces.

import net.rim.device.api.browser.field2.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;

Create the application framework by extending the UiApplication class. In main(), create an instance of the new class and invoke enterEventDispatcher() to enable the application to receive events. In the application constructor, invoke pushScreen() to display the custom screen for the application. The BrowserFieldDemoScreen class represents the custom screen.

public class BrowserFieldDemo extends UiApplication
{
     public static void main(String[] args)
     {
          BrowserFieldDemo app = new BrowserFieldDemo();
          app.enterEventDispatcher();
     }

     public BrowserFieldDemo() 
     {
          pushScreen(new BrowserFieldDemoScreen());
     }
}

Create the framework for the custom screen by extending the MainScreen class.

class BrowserFieldDemoScreen extends MainScreen
{
     public BrowserFieldDemoScreen()
     {
     }
}

In the screen constructor, create an instance of the BrowserFieldConfig class.

BrowserFieldConfig myBrowserFieldConfig = new BrowserFieldConfig();

In the screen constructor, invoke BrowserFieldConfig.setProperty() to specify a property of the BrowserField. The first parameter in setProperty() specifies the property, and the second parameter specifies the value of the property. For example, the following code sample specifies the NAVIGATION_MODE property of a BrowserField object:

myBrowserFieldConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE,
BrowserFieldConfig.NAVIGATION_MODE_POINTER);

In the screen constructor, create an instance of the BrowserField class that uses the configuration that you defined.

BrowserField browserField = new BrowserField(myBrowserFieldConfig);

Send form data to a web address in a browser field

Import the required classes and interfaces.

import net.rim.device.api.browser.field2.*;
import net.rim.device.api.io.http.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import java.lang.*;
import java.util.*;

Create the application framework by extending the UiApplication class. In main(), create an instance of the new class and invoke enterEventDispatcher() to enable the application to receive events. In the application constructor, invoke pushScreen() to display the custom screen for the application. The BrowserFieldDemoScreen class represents the custom screen.

public class BrowserFieldDemo extends UiApplication
{
     public static void main(String[] args)
     {
          BrowserFieldDemo app = new BrowserFieldDemo();
          app.enterEventDispatcher();
     }

     public BrowserFieldDemo() 
     {
          pushScreen(new BrowserFieldDemoScreen());
     }
}

Create the custom screen by extending the MainScreen class.

class BrowserFieldDemoScreen extends MainScreen
{
     public BrowserFieldDemoScreen()
     {
     }
}

In the screen constructor, create an instance of the BrowserField class.

BrowserField browserField = new BrowserField();

In the screen constructor, invoke add() to add the BrowserField object to the screen.

add(browserField);

In the screen constructor, create a String object that contains the base web address of the web page that you are sending the form data to.

String baseURL = "http://www.blackberry.com";

In the screen constructor, create a String that specifies the form data that your application sends to the web page.

String postData = "fieldname1=value1&fieldname2=value2";

In the screen constructor, create a Hashtable object to store the header information for the form data.

Hashtable header = new Hashtable();

In the screen constructor, invoke Hashtable.put() to specify the header information for the form data.

header.put("Content-Length", "" + postData.length());
header.put("Content-Type", "application/x-www-form-urlencoded");

In the screen constructor, invoke BrowserField.requestContent() to send the form data to the web page and display the web page.

browserField.requestContent(baseURL, postData.getBytes(), new HttpHeaders(header));