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: 47
Registered: ‎11-11-2009
My Device: Not Specified
Accepted Solution

Custom BrowserField not displaying in Real Device, Simulator only

[ Edited ]

Hey guys, I got some piece of code that I can use to display HTML data stored in a variable. It worked fine in Simulator, but when I tried to display it in a real device (I'm using 8900) it doesn't show anything.

 

 

Here is the code (HTMLField.java): (I'm using the code from here: http://supportforums.blackberry.com/t5/Java-Development/Display-HTML-in-a-Screen-Field/td-p/335074/p...)

 

package app;

import java.io.IOException;

import javax.microedition.io.HttpConnection;

import net.rim.device.api.browser.field.BrowserContent;
import net.rim.device.api.browser.field.Event;
import net.rim.device.api.browser.field.RedirectEvent;
import net.rim.device.api.browser.field.RenderingApplication;
import net.rim.device.api.browser.field.RenderingException;
import net.rim.device.api.browser.field.RenderingOptions;
import net.rim.device.api.browser.field.RenderingSession;
import net.rim.device.api.browser.field.RequestedResource;
import net.rim.device.api.system.Application;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Status;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.io.http.HttpHeaders;

/**
 * A field which displays HTML content.
 * @author Febiyan Rachman
 */
public class HTMLField extends VerticalFieldManager implements RenderingApplication
{
	private String data;
    private static final String REFERER = "referer";
    private RenderingSession renderingSession;
    private HttpConnection currentConnection;
    private Field field;
    
    /**
     * Default constructor
     * @param data
     */
    public HTMLField(String data) 
	{
        this.data = data;
        renderingSession = RenderingSession.getNewInstance();
        // Enable JavaScript
        renderingSession.getRenderingOptions().setProperty(RenderingOptions.CORE_OPTIONS_GUID, RenderingOptions.JAVASCRIPT_ENABLED, true);
    }
    
    /**
     * Start parsing thread
     */
    public void parseHTML()
    {
    	if(!data.startsWith("<html") && !data.startsWith("<!DOCTYPE")) data = "<html>"+data+"</html>";
    	HTMLFieldConnection thread = new HTMLFieldConnection(data, null, this);
        thread.start();
    }
    
    /**
     * Parse HTML
     * @param data
     */
    public void parseHTML(String data)
    {
    	this.data = data;
    	if(!data.startsWith("<html") && !data.startsWith("<!DOCTYPE")) data = "<html>"+data+"</html>";
    	HTMLFieldConnection thread = new HTMLFieldConnection(data, null, this);
        thread.start();
    }
    
	/**
	 * 
	 * @param connection
	 * @param event
	 */
    public void processConnection(HttpConnection connection, Event event) 
    {
        // Cancel previous request
        if (currentConnection != null) {
            try 
            {
                currentConnection.close();
            } 
            catch (IOException e1) {
            }
        }
        
        // Set the current connection to the created connection
        currentConnection = connection;
        
        BrowserContent browserContent = null;

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

            if (browserContent != null) 
            {
            	// Create a field which displays the HTML content
                Field newField = browserContent.getDisplayableContent();
                // Add field to this manager
                if (newField != null) 
                {
                	if(field != null)
                	{
                		synchronized (UiApplication.getEventLock()) 
                		{
							replace(field, newField);
							field = null;
	                		field = newField;
						}
                		
                	}
                	else
                	{
                		synchronized (UiApplication.getEventLock()) 
                		{
							add(newField);
							field = newField;
						}
             
                	}
                }
               
                // Finish!
                browserContent.finishLoading();
            }
        }
        catch (RenderingException renderingException) 
        {
            System.out.println("RenderingException : " + renderingException);
        } 
        catch (Exception exception) 
        {
            System.out.println("Exception : " + exception);
            exception.printStackTrace();
        }
    }

    /**
     * @see net.rim.device.api.browser.field.RenderingApplication#eventOccurred(net.rim.device.api.browser.field.Event)
     */
    public Object eventOccurred(Event event) 
    {
        int eventId = event.getUID();

        switch (eventId) 
        {
            case Event.EVENT_URL_REQUESTED: 
            {
                HTMLFieldConnection thread = new HTMLFieldConnection(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&colon;
                        break;
                    case RedirectEvent.TYPE_META:
                        // MSIE and Mozilla don't send a Referrer for META Refresh.
                        referrer = null;
                        break;
                    case RedirectEvent.TYPE_300_REDIRECT:
                        // MSIE, Mozilla, and Opera all send the original
                        // request's Referrer as the Referrer 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);
                HTMLFieldConnection thread = new HTMLFieldConnection(this.data, event, this);
                thread.start();
                break;

            }
            case Event.EVENT_CLOSE:
                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;
    }

	public int getAvailableHeight(BrowserContent browserContent) 
	{
		return 0;
	}

	public int getAvailableWidth(BrowserContent browserContent) 
	{
		return 0;
	}

	public String getHTTPCookie(String url) 
	{
		return null;
	}

	public int getHistoryPosition(BrowserContent browserContent) 
	{
		return 0;
	}

	public HttpConnection getResource(RequestedResource resource, BrowserContent referrer) 
	{
		return null;
	}

	public void invokeRunnable(Runnable runnable) 
	{
	}

}

 

 

BrowserApp.java

 

package app;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.container.MainScreen;

/**
 * @author Febiyan Rachman
 */
public class BrowserApp extends UiApplication implements FieldChangeListener
{
	HTMLField htmlField;
	EditField textField;

	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		BrowserApp app = new BrowserApp();
		app.enterEventDispatcher();
	}
	
	/**
	 * Default constructor
	 */
	public BrowserApp()
	{
		MainScreen screen = new MainScreen();
		screen.setTitle("Browser Test");
		htmlField = new HTMLField("");
		//screen.add();
		textField = new EditField("Tes : ", "");
		ButtonField button = new ButtonField("click", ButtonField.CONSUME_CLICK);
		button.setChangeListener(this);
		screen.add(textField);
		screen.add(htmlField);
		screen.add(button);
		pushScreen(screen);
	}
	
	// parseHTML and display it
	public void fieldChanged(Field field, int context) 
	{
		// TODO Auto-generated method stub
		htmlField.parseHTML(textField.getText());
	}

}

 

 

HTMLFieldConnection.java

 

package app;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.io.HttpConnection;

import net.rim.device.api.browser.field.Event;

public class HTMLFieldConnection extends Thread implements HttpConnection 
{
    private long streamLength = 7000;
    private DataInputStream dataInput;
    private InputStream in;
    private String encoding = "text/html";
    private HTMLField htmlField;
    private Event event;
    private String data;
    
    /**
     * Default
     * @param data
     * @param event : event object for eventOccured() function
     * @param htmlField : HTML display field
     */
    public HTMLFieldConnection(String data, Event event, HTMLField htmlField)
    {
    	this.data = data;
        this.htmlField = htmlField;
        this.event = event;
    }

    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());
    }

    public void run() 
    {
    	 try 
         {
             in = new ByteArrayInputStream(data.getBytes("UTF-8"));
             dataInput = new DataInputStream(in);
         } 
         catch (Exception e) 
         {
             System.out.println("HttpConnectionImpl : Exception : " + e);
         }
        this.htmlField.processConnection(this, this.event);
    }
}

 

 

 

Is there anything wrong with it?

 

Developer
Posts: 47
Registered: ‎11-11-2009
My Device: Not Specified

Re: Custom BrowserField not displaying in Real Device, Simulator only

Bump. :smileyhappy:

 

Sorry if the post is lengthy. Please try to copy and paste the code, and run it in both (simulator and device)

Contributor
Posts: 11
Registered: ‎08-18-2010
My Device: Not Specified

Re: Custom BrowserField not displaying in Real Device, Simulator only

Hi i am getting NullPointer Exception and NO stack Trace exception in HTMLField class + in processConnection method.plz help me.

 

Thanks in advance

A.suman

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Custom BrowserField not displaying in Real Device, Simulator only

I don't have the time to run your code but in general these things are touchy.

You need to have diagnostics in place for debuggin IO on a real phone given

that you can't get programmatic stack traces. Generally if you put the older ones on

display stack too late the rendering is messed up and there are other quirks if you

try to use Screen rather than MainScreen ( if you step throught the code you may notice

that undocumented methods in Main or Screen seem to get called a lot ...).

 

Also I have had seveal failures on older simulator that either didn't occur

or manifest differently on a real phone, likely due to timing and threading errors.

You can step through the browser code and find mthod sigs even without

source code suggesting that ordering of spawning threads is  inherently unsafe.

 

 

 

Developer
Posts: 47
Registered: ‎11-11-2009
My Device: Not Specified

Re: Custom BrowserField not displaying in Real Device, Simulator only

Yes, as I finally tried over and over, it seems that the OS 5 doesn't support old tricks like this, it has its own browserfield2. :smileyhappy: