If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

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
nish1013
Posts: 253
Registered: ‎08-14-2010
My Device: Not Specified
Accepted Solution

BrowserField help

[ Edited ]

I have follwing type of text 

 

<p> sample html text .</p> 

 

I passed this to BrowserField and the output is 

 

<p> sample html text .</p>

 

 

BUT I NEED TO PARSE THE HTML COMPLETELY AND GET THE OUTPUT.

 

can anyone help?

 

cheers

Please use plain text.
Developer
PoojaMaheshwari
Posts: 141
Registered: ‎02-10-2010
My Device: 9500
My Carrier: Airtel

Re: BrowserField help

You need not do any parsing for HTML. HTML content is supported by default. Please refer to the below sample code retrieving and displaying  Browser content:

 

RenderingThread.java:

 

package test;

import java.io.IOException;
import java.io.OutputStream;

import javax.microedition.io.Connector;
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.RenderingException;
import net.rim.device.api.browser.field.RenderingSession;
import net.rim.device.api.io.http.HttpHeaders;
import net.rim.device.api.io.http.HttpProtocolConstants;
import net.rim.device.api.system.Application;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.util.StringUtilities;

public class RenderingThread extends Thread {
	
	RenderingSession _renderingSession = null;
	
	String _url;
	
	HttpHeaders _requestHeaders;
	
	byte[] postData;
	
	MainScreen _mainScreen;
	
	public RenderingThread(MainScreen screen,String url, HttpHeaders requestHeaders, byte[] postData,
            Event event) {
		_url = url;
		_mainScreen = screen;
	}
	
	 public void run() {
		    HttpConnection connection = ConnectionUtil.getConnection(_url,_requestHeaders,postData);

			makeRequest(connection, null);     
	    }
	 


		private void makeRequest(HttpConnection connection, Event e) {

			// cancel previous request
			BrowserContent browserContent = null;

			try {
				_renderingSession = RenderingSession.getNewInstance();
				browserContent = _renderingSession.getBrowserContent(connection,
						new RenderingAppImpl(_mainScreen), e);

				if (browserContent != null) {

					Field field = browserContent.getDisplayableContent();

					if (field != null) {
						synchronized (Application.getEventLock()) {
							_mainScreen.deleteAll();
							_mainScreen.add(field);
						}
					}

					browserContent.finishLoading();
				}

			} catch (RenderingException re) {

			} finally {

			}

		}

}

 

RenderingAppImpl.java:

 

package test;

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.RenderingApplication;
import net.rim.device.api.browser.field.RequestedResource;
import net.rim.device.api.browser.field.UrlRequestedEvent;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.container.MainScreen;

public class RenderingAppImpl implements RenderingApplication {

	MainScreen _screen ;
	
	public RenderingAppImpl(MainScreen screen ) {
	_screen = screen;
	}
	
	
	public Object eventOccurred(Event event) {
		// TODO Auto-generated method stub
		Log.debug("Event occured ");
		
		int eventId = event.getUID();

		switch (eventId) {

		case Event.EVENT_URL_REQUESTED: {

			UrlRequestedEvent urlRequestedEvent = (UrlRequestedEvent) event;
			String absoluteUrl = urlRequestedEvent.getURL();
			Log.debug("Event occured " + absoluteUrl);
			 
			RenderingThread thread = new RenderingThread(_screen,absoluteUrl, urlRequestedEvent.getHeaders(),
			urlRequestedEvent.getPostData(),

			event);
			thread.start();

			break;

		}
		default:
		}
		
		return null;
	}

	/**
	 * @see net.rim.device.api.browser.RenderingApplication#getAvailableHeight(net.rim.device.api.browser.BrowserContent)
	 */
	public int getAvailableHeight(BrowserContent browserField) {
		// field has full screen
		Log.debug("pixels " + Display.getHeight());
		
		
		return Display.getHeight();
	}

	/**
	 * @see net.rim.device.api.browser.RenderingApplication#getAvailableWidth(net.rim.device.api.browser.BrowserContent)
	 */
	public int getAvailableWidth(BrowserContent browserField) {
		// field has full screen
		return Display.getWidth();
	}

	/**
	 * @see net.rim.device.api.browser.RenderingApplication#getHistoryPosition(net.rim.device.api.browser.BrowserContent)
	 */
	public int getHistoryPosition(BrowserContent browserField) {
		// no history support
		return 0;
	}

	/**
	 * @see net.rim.device.api.browser.RenderingApplication#getHTTPCookie(java.lang.String)
	 */
	public String getHTTPCookie(String url) {
		// no cookie support
		return null;
	}

	public HttpConnection getResource(RequestedResource resource,
			BrowserContent referrer) {
		// TODO Auto-generated method stub

        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 = ConnectionUtil.getConnection(resource.getUrl(), resource.getRequestHeaders(), null);

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

	public void invokeRunnable(Runnable runnable) {
		(new Thread(runnable)).run();

	}

}

 

Calling point:

 

				RenderingThread thread= new RenderingThread(_screen,"http://www.google.com",null,null,null);
				thread.start();

 

Please use plain text.
Developer
nish1013
Posts: 253
Registered: ‎08-14-2010
My Device: Not Specified

Re: BrowserField help

Hi Pooja,

 

But I am using os 5.0 and BrowserField.

 

And further the data I am getting from a JSON feed , so I received it as a String  with encoded style.(so if it is got to the level <p>jjjjjjj</p> it does the job for me!)

 

any clue?

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: BrowserField help

[ Edited ]

Am I correct is saying that you are reading the data yourself, and then passing it to a BrowserField for display?

 

It looks like whatever is creating the text you want to display believes it is to be packaged in some XML or HTML format.  In that format, 'text' data must be encoded so that it is not mixed up with the actual XML or HTML 'control' characters.  So if a piece of text contains a "<" this is converted as you have seen so that it is not detected as the start of a tag.

 

You either need to decode this before passing the String to your BrowserField or get whatever has created this text to not encode it.

 

Which of these options will work for you?

 

Edit:

You might find this useful:

http://en.wikipedia.org/wiki/Character_encodings_in_HTML

Please use plain text.
Developer
nish1013
Posts: 253
Registered: ‎08-14-2010
My Device: Not Specified

Re: BrowserField help

Hi Peter,

 

It is exactly the scenario what you assumed.

 

If I can decode the text (in my code), this will work for me .

 

I looked for a way to decode the text but I could not find anyway so far.

 

please help if you know any way.

 

Cheers

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: BrowserField help

I don't have any code to do this.  Nor am I aware of any classes on the BB that will help you.

 

If you do a Google search you will find that Apache has some classes that will help.  The best 'source' I've found is here:

 

http://www.jarvana.com/jarvana/view/commons-lang/commons-lang/2.4/commons-lang-2.4-sources.jar!/org/...

 

but I didn't look very hard.  You migth find an easier to use piece of code. 

 

If you do use the papache source, all the code you need is in there, you will have to fight it a bit to get code that will run easily on a J2ME device, sorry I can't simplify that process for you. 

 

Is this enough?

 

Let us know how you get on.

Please use plain text.
Developer
nish1013
Posts: 253
Registered: ‎08-14-2010
My Device: Not Specified

Re: BrowserField help

[ Edited ]

Hey all,

 

Finally I found it here :Clap:

 

Many Thanks to Réal Gagnon <www.rgagnon.com>

 

http://www.rgagnon.com/javadetails/java-0307.html

 

}

 

 

public class StringUtils {

  private StringUtils() {}

  private static String [][] htmlEscape =
     {{  "&lt;"     , "<" } ,  {  "&gt;"     , ">" } ,
      {  "&amp;"    , "&" } ,  {  "&quot;"   , "\"" } ,
      {  "&agrave;" , "à" } ,  {  "&Agrave;" , "À" } ,
      {  "&acirc;"  , "â" } ,  {  "&auml;"   , "ä" } ,
      {  "&Auml;"   , "Ä" } ,  {  "&Acirc;"  , "Â" } ,
      {  "&aring;"  , "å" } ,  {  "&Aring;"  , "Å" } ,
      {  "&aelig;"  , "æ" } ,  {  "&AElig;"  , "Æ" } ,
      {  "&ccedil;" , "ç" } ,  {  "&Ccedil;" , "Ç" } ,
      {  "&eacute;" , "é" } ,  {  "&Eacute;" , "É" } ,
      {  "&egrave;" , "è" } ,  {  "&Egrave;" , "È" } ,
      {  "&ecirc;"  , "ê" } ,  {  "&Ecirc;"  , "Ê" } ,
      {  "&euml;"   , "ë" } ,  {  "&Euml;"   , "Ë" } ,
      {  "&iuml;"   , "ï" } ,  {  "&Iuml;"   , "Ï" } ,
      {  "&ocirc;"  , "ô" } ,  {  "&Ocirc;"  , "Ô" } ,
      {  "&ouml;"   , "ö" } ,  {  "&Ouml;"   , "Ö" } ,
      {  "&oslash;" , "ø" } ,  {  "&Oslash;" , "Ø" } ,
      {  "&szlig;"  , "ß" } ,  {  "&ugrave;" , "ù" } ,
      {  "&Ugrave;" , "Ù" } ,  {  "&ucirc;"  , "û" } ,
      {  "&Ucirc;"  , "Û" } ,  {  "&uuml;"   , "ü" } ,
      {  "&Uuml;"   , "Ü" } ,  {  "&nbsp;"   , " " } ,
      {  "&copy;"   , "\u00a9" } ,
      {  "&reg;"    , "\u00ae" } ,
      {  "&euro;"   , "\u20a0" }
     };

  public static final String unescapeHTML(String s, int start){
     int i, j, k;

     i = s.indexOf("&", start);
     start = i + 1;
     if (i > -1) {
        j = s.indexOf(";" ,i);
        /*
           we don't want to start from the beginning
           the next time, to handle the case of the &
           thanks to Pieter Hertogh for the bug fix!
        */
        if (j > i) {
           // ok this is not most optimized way to
           // do it, a StringBuffer would be better,
           // this is left as an exercise to the reader!
           String temp = s.substring(i , j + 1);
           // search in htmlEscape[][] if temp is there
           k = 0;
           while (k < htmlEscape.length) {
             if (htmlEscape[k][0].equals(temp)) break;
             else k++;
           }
           if (k < htmlEscape.length) {
             s = s.substring(0 , i)
                    + htmlEscape[k][1] + s.substring(j + 1);
             return unescapeHTML(s, i); // recursive call
           }
         }
     }
     return s;
  }

  public static void main(String args[]) throws Exception {
      // to see accented character to the console
      java.io.PrintStream ps = new java.io.PrintStream(System.out, true, "Cp850");
      String test = "&copy; 2000  R&eacute;al Gagnon &lt;www.rgagnon.com&gt;";
      ps.println(test + "\n-->\n" +unescapeHTML(test, 0));

      /*
         output :
         &copy; 2000  R&eacute;al Gagnon &lt;www.rgagnon.com&gt;
         -->
         © 2000  Réal Gagnon <www.rgagnon.com>
      */
  } 
Please use plain text.