0

Is there any way to show LOCAL html string in my BB application?

For example: I have a string

"<b>text-text-text</b> <A href="http://stackoverflow.com">link</A>"

How can I parse and show it as

text-text-text link?

Thanks.

oxigen
  • 6,263
  • 3
  • 28
  • 37

4 Answers4

2

You can use a BrowserField to render content in your application as a Field. In order to render HTML provided as a String rather than a URL, see this answer which involves mocking an HttpConnection.

Community
  • 1
  • 1
Marc Novakowski
  • 44,628
  • 11
  • 58
  • 63
1
import javax.microedition.io.Connection;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import java.util.*;
import java.io.*;

import net.rim.blackberry.api.browser.Browser;
import net.rim.blackberry.api.browser.BrowserSession;
import net.rim.device.api.ui.*;
import net.rim.device.api.browser.field.*;
import net.rim.device.api.browser.plugin.BrowserPageContext;
import net.rim.device.api.io.Base64OutputStream;
import net.rim.device.api.io.http.HttpHeaders;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.system.Application;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Status;
import net.rim.device.api.ui.component.TextField;



final class bFieldExample extends UiApplication 

{
    private static final String REFERER = "referer";

    public static final HttpConnection Utilities = null;

//  private static final HttpConnection HttpConnection = null;  

    private RenderingSession rs;  

    private MainScreen ms;

    private HttpConnection  conn;

    public static void main(String[] args)
    {
        bFieldExample app = new bFieldExample();
        app.enterEventDispatcher();

    }

    public bFieldExample()
    {
        ms = new MainScreen();
        pushScreen(ms);
        rs = RenderingSession.getNewInstance();
        String s = "<b>text-text-text</b> <A href="http://stackoverflow.com">link</A>"

        //rs.getRenderingOptions().setProperty(RenderingOptions.CORE_OPTIONS_GUID,RenderingOptions.JAVASCRIPT_ENABLED, true);
        //LabelField l = new LabelField("Browser",LabelField.ELLIPSIS|LabelField.USE_ALL_WIDTH);
        ms.setTitle(new LabelField("Browser",LabelField.ELLIPSIS|LabelField.USE_ALL_WIDTH));        
        myThread thread = new myThread(s);
        thread.start();
    }


    class myThread extends Thread
    {

        private String _url;

        myThread(String url)
        {
                _url=url;

        }

        public void run()
        {
            try {

                ByteArrayOutputStream output = new ByteArrayOutputStream();
                Base64OutputStream boutput = new Base64OutputStream( output );                
                output.write( "data:text/html;base64,".getBytes() );
                boutput.write( _url.getBytes() );
                boutput.flush();
                boutput.close();
                output.flush();
                output.close();

                BrowserSession bSession = Browser.getDefaultSession();
                bSession.displayPage( output.toString() );

                } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   

        }
    }

}
Abel
  • 56,041
  • 24
  • 146
  • 247
Thiru
  • 11
  • 1
  • 2
    That will open a new browser -- I think the author of the question is looking for a solution that renders the HTML inside the application itself. – Marc Novakowski Dec 30 '09 at 17:21
1
myBrowserField.displayContent("<b>text-text-text</b> <A href="http://stackoverflow.com">link</A>", "http://localhost");
bogdan
  • 338
  • 6
  • 10
0

Try this one :

BrowserField myBrowserField;
myBrowserField = new BrowserField();
add(myBrowserField);
String content ="<html><head></head><body>"+
    "<b>text-text-text</b> <A href="http://stackoverflow.com">link</A>"+
    "</body></html>";
myBrowserField.displayContent(content,"");//"http://localhost"
Galvion
  • 1,353
  • 7
  • 23
  • 35