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

Invoke the browser with raw HTML

by BlackBerry Development Advisor ‎02-12-2010 03:37 PM - edited ‎02-17-2010 09:52 AM (3,262 Views)


Summary


This article applies to BlackBerry® Device Software 4.2 and later.




Description


With BlackBerry Device Software 4.2, you provide raw HTML to invoke the BlackBerry Browser instead of providing a URL to fetch content. This allows you to use the BlackBerry Browser for locally stored content using standards-based HTML for display.


Note: The Browser Field classes can be used to display local HTML content, but this method would require a non-trivial amount of implementation work.


The mechanism for using the BlackBerry Browser to invoke the browser with raw HTML content is shown in the sample code below. The key aspect of the code is the section where you pass in the following string to the browser at the start of the data sequence:


data:text/html;base64,

/*
* RawHTMLBrowserSample.java
*
* © Research In Motion Limited, 2007
* Confidential and proprietary.
*/

import java.io.*;
import javax.microedition.io.*;
import net.rim.blackberry.api.browser.*;
import net.rim.device.api.io.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.util.*;

/**
* A simple example of how to invoke the BlackBerry Browser using
* raw HTML data.
*/
public class RawHTMLBrowserSample extends UiApplication
{
public static void main( String[] args )
{
RawHTMLBrowserSample sample = new RawHTMLBrowserSample();
sample.enterEventDispatcher();
}

/**
* Create the splash screen and start off the networking
* thread that will eventually invoke the browser.
*/
public RawHTMLBrowserSample()
{
MainScreen splashScreen = new MainScreen();
splashScreen.add( new RichTextField( "Please Wait..." ));
pushScreen( splashScreen );
MyThread thread = new MyThread();
thread.start();
}
private static class MyThread extends Thread
{
public void run()
{
try {
// Open the connection and the input stream. Select your own URL.
HttpConnection connection = (HttpConnection)Connector.open( "http://www.blackberry.com:80" );
InputStream input = connection.openInputStream();
DataBuffer buffer = new DataBuffer();
byte[] temp = new byte[ 1024 ];
// Read in the web page to the DataBuffer.
for( ;; ) {
int bytesRead = input.read( temp );
if( bytesRead == -1 ) {
break;
}
buffer.write( temp, 0, bytesRead );
}
input.close();
connection.close();
// Create an output stream that will be used by the Base64 code.
ByteArrayOutputStream output = new ByteArrayOutputStream();
Base64OutputStream boutput = new Base64OutputStream( output );
// Write out the special sequence which indicates to the browser
// that it should treat this as HTML data in base64 format.
output.write( "data:text/html;base64,".getBytes() );
boutput.write( buffer.getArray() );
boutput.flush();
boutput.close();
output.flush();
output.close(); // Invoke the browser with the encoded HTML content.
BrowserSession bSession = Browser.getDefaultSession();
bSession.displayPage( output.toString() );
} catch( IOException e ) {
System.out.println( "IOException: " + e );
}
}
}
}



The browser uses the sequence to base64 decode the string following the comma and displays it in the browser. When displaying the page, images and other items do not appear because the browser cannot fetch them.


Contributors
Users Online
Currently online: 37 members 1,081 guests
Please welcome our newest community members: