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
titakalpesh
Posts: 203
Registered: ‎07-30-2009
My Device: Bold(9780),Curve(8520)
My Carrier: Vodafone

Re: Display HTML in a Screen Field

[ Edited ]

Hi I try this but not get  the MBrowserField class so it give the error in my project so can you please provide me the detail code

New Developer
dfranco
Posts: 1
Registered: ‎11-25-2009
My Device: Not Specified

Re: Display HTML in a Screen Field

Hello,

It works for me!!!  but I cant' get the image in the HTML to display. Is there anything I can do to get the images to show??

Thank you!!!

_df

Developer
BBDeveloper
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: Display HTML in a Screen Field

Chekc will this thread help you.

 

http://supportforums.blackberry.com/t5/Java-Development/Problem-of-custom-browserfield-to-show-html-...


Use Search. "Accept Solution" If the problem is resolved.
New Developer
cmouritsen
Posts: 3
Registered: ‎08-18-2009
My Device: Not Specified

Re: Display HTML in a Screen Field

This solution is working for me to display local html. Is there any way to reference a css (or javascript) file using this method?

Developer
BBDeveloper
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: Display HTML in a Screen Field

Check this Thread.

 

http://supportforums.blackberry.com/t5/Java-Development/Javascript-in-BrowserField/m-p/331860


Use Search. "Accept Solution" If the problem is resolved.
Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Display HTML in a Screen Field

This does turn out to be a reasonable way to make a gui if you want it to look like a web

app and can tolerate the speed and limitations of a browser( you wouldn't design a game

this way LOL). You can hide the actual URL details from the browser with your fake http

connections and play whatever games you need to return internal state information to

browser etc, similar to the way you can get gps info via javascript.

 

New Developer
drvais
Posts: 8
Registered: ‎01-25-2010
My Device: Storm 9350

Re: Display HTML in a Screen Field

I also cannot get the MBrowserField class to work.  Could you please post the missing code for that part?

New Developer
drvais
Posts: 8
Registered: ‎01-25-2010
My Device: Storm 9350

Re: Display HTML in a Screen Field

Oops, I figured it out.  For anyone coming across this thread and having a similar problem with MBrowserField, it's just the name of the file itself (i.e.- MBrowserField.java).

New Developer
drvais
Posts: 8
Registered: ‎01-25-2010
My Device: Storm 9350

Re: Display HTML in a Screen Field

I have been trying to follow along with the examples provided by kelumpks in this thread but I'm hitting a roadblock.  I keep getting a compiler error of:

 

cannot find symbol
symbol  : method MBrowserFieldDemo(java.lang.String,net.rim.device.api.ui.container.MainScreen)
location: class com.rim.samples.device.blackberry.browser.BrowserScreen
           MBrowserFieldDemo(p_strHTML,mainScreen);

 

when I try to use the following code:

 

UiApp...

 

public class BrowserSample extends UiApplication 
{
        public static void main(String[] args)
    {
                BrowserSample theApp = new BrowserSample();
        theApp.enterEventDispatcher();
    }
        
        public BrowserSample()
        {
                pushScreen(new BrowserScreen());
        }
}

 The modified HTTPConnection:

 

public class HttpConnectionImpl implements HttpConnection 
{
        private long streamLength = 7000; //I added this
        private DataInputStream dataInput;
        private InputStream in;
        private String encoding = "text / html";//providing space between "/" so that i can post this message.
        
        public HttpConnectionImpl(String htmlString)
        {
                try
                {
                        in = new ByteArrayInputStream(htmlString.getBytes("UTF-8"));
                        dataInput = new DataInputStream(in);
                        
                        
                }
                catch (Exception e)
                {
                        System.out.println("HttpConnectionImpl : Exception : " + e);
                }
        }
        
        public String getURL() {
        return "";
    }

    public String getProtocol() {
        return "";
    }

    public String getHost() {
        return "";
    }

    public String getFile() {
        return "";
    }

    public String getRef() {
        return "";
    }

    public String getQuery() {
        return "";
    }

    public int getPort() {
        return 0;
    }

    public String getRequestMethod() {
        return "";
    }

    public void setRequestMethod(String s) throws IOException {

    }

    public String getRequestProperty(String s) {
        return "";
    }

    public void setRequestProperty(String s, String s1) throws IOException {

    }

    public int getResponseCode() throws IOException {
        return 200;
    }

    public String getResponseMessage() throws IOException {
        return "";
    }

    public long getExpiration() throws IOException {
        return 0;
    }

    public long getDate() throws IOException {
        return 0;
    }

    public long getLastModified() throws IOException {
        return 0;
    }

    public String getHeaderField(String s) throws IOException {
        return "";
    }

    public int getHeaderFieldInt(String s, int i) throws IOException {
        return 0;
    }

    public long getHeaderFieldDate(String s, long l) throws IOException {
        return 0;
    }

    public String getHeaderField(int i) throws IOException {
        return "";
    }

    public String getHeaderFieldKey(int i) throws IOException {
        return "";
    }

    public String getType() {
        return "text/html";
    }

    public String getEncoding() {
        return encoding;
    }

    public long getLength() {
        return streamLength;
    }

    public InputStream openInputStream() throws IOException {
        return in;
    }

    public DataInputStream openDataInputStream() throws IOException {
        return dataInput;
    }

    public void close() throws IOException {

    }

    public OutputStream openOutputStream() throws IOException {
        return new ByteArrayOutputStream();
    }

    public DataOutputStream openDataOutputStream() throws IOException {
        return new DataOutputStream(new ByteArrayOutputStream());
    }
}

 

 

The modified browser:

 

public class MBrowserFieldDemo implements RenderingApplication {
    private String data;
    private static final String REFERER = "referer";
    private RenderingSession renderingSession;
    private HttpConnection currentConnection;
    private Field displayableField;
    private MainScreen mainScreen;

    public MBrowserFieldDemo(String data, MainScreen mainScreen) {
        this.data = data;
        
       // mainScreen = new MainScreen();
     //   pushScreen(mainScreen);
        
        
        
     //   this.mainScreen = mainScreen;
      
        this.mainScreen = new MainScreen();
      
        renderingSession = RenderingSession.getNewInstance();
        // enable javascript
        renderingSession.getRenderingOptions().setProperty(RenderingOptions.CORE_OPTIONS_GUID, RenderingOptions.JAVASCRIPT_ENABLED, true);
        ResourceFetchThread thread = new ResourceFetchThread(data, null, this);
        thread.start();
    }

    public void processConnection(HttpConnection connection, Event e) {

        // cancel previous request
        if (currentConnection != null) {
            try {
                currentConnection.close();
            } catch (IOException e1) {
            }
        }

        currentConnection = connection;

        BrowserContent browserContent = null;

        try {
            browserContent = renderingSession.getBrowserContent(connection, this, e);

            if (browserContent != null) {
                Field field = browserContent.getDisplayableContent();
                if (field != null) {
                    synchronized (Application.getEventLock()) {
                        this.mainScreen.add(field);
                    }
                }

                browserContent.finishLoading();
                System.out.println("Done loading>>>>>");
            }

        } catch (RenderingException re) {
            System.out.println("RenderingException : " + re);
        } catch (Exception ex) {
            System.out.println("Exception : " + ex);
            ex.printStackTrace();
        }

    }

    public Object eventOccurred(Event event) {

        int eventId = event.getUID();

        switch (eventId) {

            case Event.EVENT_URL_REQUESTED: {
                ResourceFetchThread thread = new ResourceFetchThread(data, null, this);
                thread.start();
                break;
            }
            case Event.EVENT_BROWSER_CONTENT_CHANGED: {
                break;
            }
            case Event.EVENT_REDIRECT: {

                RedirectEvent e = (RedirectEvent) event;
                String referrer = e.getSourceURL();

                switch (e.getType()) {

                    case RedirectEvent.TYPE_SINGLE_FRAME_REDIRECT:
                        // show redirect message
                        Application.getApplication().invokeAndWait(new Runnable() {
                            public void run() {
                                Status.show("You are being redirected to a different page...");
                            }
                        });

                        break;

             /*       case RedirectEvent.TYPE_JAVASCRIPT:
                        break;
                    case RedirectEvent.TYPE_META:
                        // MSIE and Mozilla don't send a Referer for META Refresh.
                        referrer = null;
                        break;
                    case RedirectEvent.TYPE_300_REDIRECT:
                        // MSIE, Mozilla, and Opera all send the original
                        // request's Referer as the Referer for the new
                        // request.
                        Object eventSource = e.getSource();
                        if (eventSource instanceof HttpConnection) {
                            referrer = ((HttpConnection) eventSource).getRequestProperty(REFERER);
                        }
                        break;*/

                }

                HttpHeaders requestHeaders = new HttpHeaders();
                requestHeaders.setProperty(REFERER, referrer);
                ResourceFetchThread thread = new ResourceFetchThread(this.data, event, this);
                thread.start();
                break;

            }
            case Event.EVENT_CLOSE:
                // TODO: close the appication
                break;

            case Event.EVENT_SET_HEADER:        // no cache support
            case Event.EVENT_SET_HTTP_COOKIE:   // no cookie support
            case Event.EVENT_HISTORY:           // no history support
            case Event.EVENT_EXECUTING_SCRIPT:  // no progress bar is supported
            case Event.EVENT_FULL_WINDOW:       // no full window support
            case Event.EVENT_STOP:              // no stop loading support
            default:
        }

        return null;
    }

    /**
     * @see
     */
    public int getAvailableHeight(BrowserContent browserField) {
        // field has full screen
        return Graphics.getScreenHeight();
    }

    /**
     * @see
     */
    public int getAvailableWidth(BrowserContent browserField) {
        // field has full screen
        return Graphics.getScreenWidth();
    }

    /**
     * @see
     */
    public int getHistoryPosition(BrowserContent browserField) {
        // no history support
        return 0;
    }


    public String getHTTPCookie(String url) {
        // no cookie support
        return null;
    }


    public HttpConnection getResource(RequestedResource resource, BrowserContent referrer) {

        if (resource == null) {
            return null;
        }

        // check if this is cache-only request
        if (resource.isCacheOnly()) {
            // no cache support
            return null;
        }

        String url = resource.getUrl();

        if (url == null) {
            return null;
        }

        // if referrer is null we must return the connection
        if (referrer == null) {
//            HttpConnection connection = Utilities.makeConnection(resource.getUrl(), resource.getRequestHeaders(), null);
            HttpConnection connection = new HttpConnectionImpl(url);
            return connection;

        } /*else {

            // if referrer is provided we can set up the connection on a separate thread
            SecondaryResourceFetchThread.enqueue(resource, referrer);

        }*/

        return null;
    }

    /**
     * @see
     */
    public void invokeRunnable(Runnable runnable) {
        (new Thread(runnable)).run();
    }

    public Field getDisplayableField() {
        if (displayableField == null)
            displayableField = new NullField();
        return displayableField;
    }
}


class ResourceFetchThread extends Thread {

    private MBrowserFieldDemo application;
    private Event event;
    private String data;

    ResourceFetchThread(String data, Event event, MBrowserFieldDemo application) {
        this.data = data;
        this.application = application;
        this.event = event;
    }

    public void run() {
        HttpConnection connection = new HttpConnectionImpl(this.data);
        this.application.processConnection(connection, this.event);
    }
}

 

 

and the browser screen java which is where I'm getting the compiler error....

 

 

public class BrowserScreen extends MainScreen
{
        
        private MainScreen mainScreen;  
       
       
               
        public BrowserScreen()
        {
         

         
         
         
         LabelField title = new LabelField("Hello World Demo" , LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
         setTitle(title);
         add(new RichTextField("Hello World!" ,Field.NON_FOCUSABLE));
         add(new ActiveRichTextField("<p>abcd</p><p>amber&apos; work is moving ahead</p>"));
         String test = "<p>abcd</p><p>amber&apos; work is moving ahead</p>";
     
        
        mainScreen = new MainScreen(); 
        
        
         try
         {
             
            MBrowserFieldDemo_study(test, mainScreen);  //passing the html string and a Mainscreen
             
                      
         }catch (Exception e)
         {
                        System.out.println("Exception rendering "+e.getMessage());
                }
                
                
        }
       
 }

 I don't know why the compiler isn't "seeing" MBrowserFieldDemo, as I made sure to put all four files in the same package.  I'm really frustrated and I'm turning to the community to help me make these examples work so I can go on to integrate an in-app browser to view local html files.  Many, many thanks in advance!

 

 

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Display HTML in a Screen Field

Have you checked that the package names are all the same?