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
Highlighted
New Developer
Posts: 13
Registered: ‎07-25-2013
My Device: Blackberry Curve 9300
My Carrier: MTN
Accepted Solution

Browserfield generates Internal Server Error

Hi,

 

I implemented an example using the guide on this link and its working quite right, but then i observed something. If i click a link from within the browserfield to a non html resource, say mp3  e.g http://site.com/file.mp3 or the server redirects or throws a non - html response to the browserfield, i get an internal server error.

 

I have attempted the same in the native browser and all works fine.

 

So i am thinking, it is possible to intercept a request when a link is clicked and open the native browser instead when a particular url pattern is detected?

 

Also, im finding it difficult to change the user-agent of the browserfield, here's what ive written

===========

 BrowserFieldConfig config = new BrowserFieldConfig();
       config.setProperty(BrowserFieldConfig.VIEWPORT_WIDTH, new Integer(Display.getWidth()));
         config.setProperty(BrowserFieldConfig.INITIAL_SCALE, new Float(1.0));
         config.setProperty(BrowserFieldConfig.USER_SCALABLE, Boolean.FALSE);
         config.setProperty(BrowserFieldConfig.ENABLE_COOKIES,Boolean.TRUE);
         config.setProperty(BrowserFieldConfig.JAVASCRIPT_ENABLED, Boolean.TRUE);
         config.setProperty(BrowserFieldConfig.NAVIGATION_MODE,     BrowserFieldConfig.NAVIGATION_MODE_POINTER);
         config.setProperty(BrowserFieldConfig.USER_AGENT, "MyCustomUAString ");        


         BrowserField browserField = new BrowserField(config);

==============

 

Also, i would also like  to be able to hide the progess bar when the browserfield is not loading any page, so that it only shows when it is need to (ie when a link is clicked)

 

Thanks

 

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

Re: Browserfield generates Internal Server Error

[ Edited ]

Quest 1 - Redirecting to native Browser.

 

This is actually not too hard, don't have time for detail here, so I hope that I can give you enough to get you going. 

 

There are two places you can hook a 'listener' in that will get invoked when the user requests some resource. 

 

1) The better documented one is the protocolManager, and here is an example of using this for a slightly different purpose to what you want.  But I suspect you will get the idea. 

http://supportforums.blackberry.com/t5/Java-Development/How-to-Implement-a-Web-Cache-for-Your-Browse...

Sorry I can't remember which of the two methods you will override to intercept as you require - try it to figure it out. 

 

2) The other option is to Implement a ConnectionManager.  You hook that in in much the same way, and then override

public InputConnection makeRequest(BrowserFieldRequest request)

 When hooking this in, you have to have access to the standard Connection Manager, as you want it to provide the resources normally.  Here is some sample code.  This has been cut/pasted from some of my code, hopefully it makes sense:

  BrowserFieldConfig bfconfig = this.browserField.getConfig();
  standardBFConnectionManager = (BrowserFieldConnectionManager) bfconfig.getProperty(BrowserFieldConfig.CONNECTION_MANAGER);
  if ( standardBFConnectionManager == null ) {
   standardBFConnectionManager = this.browserField.getConnectionManager();
  }
  if ( standardBFConnectionManager != null ) {
   bfconfig.setProperty(BrowserFieldConfig.CONNECTION_MANAGER, this);
  }

 

Now the trick with both options is "what do you return?" - you just can't start the external Browser, you actually have to return something.  This is simpler than you would think, here is some sample code for the ConnectionMaanger option:

 

public InputConnection makeRequest(BrowserFieldRequest request)

            throws Exception {

final String requestURL = request.getURL();

if (requestURL.startsWith(....) ) {

// redirect request to Browser

....Code to redirect to Browser

String htmlToDisplay =

"<html><body>" +

"<center><font color=\"blue\"><br/><b>Redirecting to Browser</b></font></center><br/>" +

"</body></html>";

return new BrowserFieldResponse(requestURL, htmlToDisplay.getBytes(), HttpProtocolConstants.CONTENT_TYPE_TEXT_HTML);

}

return standardBFConnectionManager.makeRequest(request);

 

Similar code will be used in both cases.

 

Re passing the URL on the Browser, there is a KB article out there that tells you how to do that,  Search.

 

Hope this is enough, sorry it is rushed. 

New Developer
Posts: 13
Registered: ‎07-25-2013
My Device: Blackberry Curve 9300
My Carrier: MTN

Re: Browserfield generates Internal Server Error

Thanks Peter, i will implement your suggestion and get back..

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

Re: Browserfield generates Internal Server Error

Never answered question 2!

 

To remove Fields like that, you have to physically delete them - to put them back add them again.  You will need to do this in your BrowserListener - and I think you will need to use the invokeLater() method to get this running on the Event Thread. 

 

Hope that makes sense.

 

PS. Edited the last post to fix some typos. 

New Developer
Posts: 13
Registered: ‎07-25-2013
My Device: Blackberry Curve 9300
My Carrier: MTN

Re: Browserfield generates Internal Server Error

Hi Peter,

Please forgive me for not responding quickly, but i was searching around ideas to help me implement the suggestion you wrote up there (Im very fresh to bb dev). I tried the protocol controller usin the cache example from the link you posted, but then it happened that the browserfield doesnt display contents any longer, except raw html source code. so i just undo that.

 

After googling, i came across some write-up about browser plugin. The argument from there was that the browserfield just dnt know what to do with some mime type, so it calls some other program to handle it. that one has has to build a custome mim type  as a plugin and register it. Saw an example of a jav source (text) viewer in the examples of the eclipse plugin, but i dont know how to turn it to suppost mp3

 

...

Any help will be appreciated , please

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

Re: Browserfield generates Internal Server Error

Some sample code.  If you can't figure out what it is supposed to do, please ask. Formatting went squiffy, sorry about that.

 

BTW, I really struggle to test BrowserField processing on SImulators, they seem flaky and sometimes I can't debug.  The one I normally use is actually an OS 5.0 9300 Simulator. 

public final class BrowserFieldScreen extends MainScreen

                                      implements BrowserFieldConnectionManager {

private BrowserField _bf2;

private BrowserFieldConnectionManager standardBFConnectionManager;

    

    public BrowserFieldScreen() {       

       

        super();

    _bf2 = new BrowserField();

    this.add(_bf2);

    // hook in our ConnectinManager

    standardBFConnectionManager = _bf2.getConnectionManager();

    BrowserFieldConfig bfconfig = _bf2.getConfig();

    bfconfig.setProperty(BrowserFieldConfig.CONNECTION_MANAGER, this);

    _bf2.requestContent("http://www.google.co.uk");

    }

// Implementation of BrowserFieldConnectionManager

public InputConnection makeRequest(BrowserFieldRequest request) {

 String requestURL = request.getURL();

 System.out.println("Requesting: " + requestURL);

 boolean toBrowser = false;

 if ( requestURL.endsWith("mp3") ) {

  toBrowser = true;

 }

 if ( requestURL.indexOf("bbc.co.uk") > 0 ) {

  toBrowser = true;

 }

 if ( toBrowser ) {

  BrowserSession browserSess = Browser.getDefaultSession();

            if ( !requestURL.startsWith("http") ) {

            requestURL = "http://" + requestURL;

            }

            browserSess.displayPage(requestURL);

  String htmlToDisplay =

    "<html><body>" +

    "<center><font color=\"blue\"><br/><b>Displaying in Browser</b></font></center><br/>" +

    "</body></html>";

  return new BrowserFieldResponse(requestURL, htmlToDisplay.getBytes(), HttpProtocolConstants.CONTENT_TYPE_TEXT_HTML);

}

try {

 return standardBFConnectionManager.makeRequest(request);

} catch (Exception e) {

 System.out.println("Exception: " + e.toString() );

}

String htmlToDisplay =

 "<html><body>" +

 "<center><font color=\"blue\"><br/><b>Exception</b></font></center><br/>" +

 "</body></html>";

return new BrowserFieldResponse(requestURL, htmlToDisplay.getBytes(), HttpProtocolConstants.CONTENT_TYPE_TEXT_HTML);

}

    MenuItem backMenuItem = new MenuItem("Back", 110, 10) {

        public void run() {

        _bf2.back();

        }

    };

    MenuItem forwardMenuItem = new MenuItem("Forward", 110, 10) {

        public void run() {

        _bf2.forward();

        }

    };

    protected void makeMenu(Menu menu, int instance) {

        if (_bf2.getHistory().canGoBack()) {

            menu.add(backMenuItem);

            menu.setDefault(backMenuItem);

        }

        if (_bf2.getHistory().canGoForward()) {

                menu.add(forwardMenuItem);

                menu.setDefault(forwardMenuItem);

        }

    }

}

New Developer
Posts: 13
Registered: ‎07-25-2013
My Device: Blackberry Curve 9300
My Carrier: MTN

Re: Browserfield generates Internal Server Error

Thanks man, really appreciate your effort...

New Developer
Posts: 13
Registered: ‎07-25-2013
My Device: Blackberry Curve 9300
My Carrier: MTN

Re: Browserfield generates Internal Server Error

Hi,

 

i know i had already marked this as answered but i ran into a little end. I observed that once i attached the connection manager to the browserfield, it worked well (as i wanted it in the request above). But then as soon as i try to set the USER_AGENT, this issue came up.

 

Let me describe what happened..

I attached the controller

 

BrowserField _bf2 = new BrowserField();

 

standardBFConnectionManager = _bf2.getConnectionManager();
        BrowserFieldConfig bfconfig = _bf2.getConfig();
       
        bfconfig.setProperty(BrowserFieldConfig.CONNECTION_MANAGER, this);

 

And then i set the user-agent..

          bfconfig.setProperty(BrowserFieldConfig.USER_AGENT,

                   " CustomUserAgentString  ");

 

So when i loaded the app on the device, ANY URL it request, i get a dialog screen telling me that the requested content cannot be displayed. When i try to save the content, the filename is .rhtml

 

====

This is what i've tried to be sure the issue came up from using the connection manager and trying to set the user agent together..

 

The APP WORKS If

1. i set the user agent working without the connetion manager.

2. Attach the connection manager without changing the user-agent string

 

The APP mis-behaves as i described above if.

I set the user agent and attach the connection manager.

 

Thank you

 

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

Re: Browserfield generates Internal Server Error

I use a Connection Manager as described with a custom user agent.  Is there any way you can tell us the user agent so that we can test it?  If you can't tell us your specific user agent, can you try with a more generic User Agent to see if the problem is related to your specific User Agent or any User Agent. 

New Developer
Posts: 13
Registered: ‎07-25-2013
My Device: Blackberry Curve 9300
My Carrier: MTN

Re: Browserfield generates Internal Server Error

This is the user agent string i am trying to set..

 

bfconfig.setProperty(BrowserFieldConfig.USER_AGENT, " BlackBerryBrowserField fromApp/VMLAppCall  ");