views:

1988

answers:

2

I want to be able to display some simple chunks of HTML in my native BlackBerry app, NOT returned from a URL. This is similar to existing Stackoverflow questions (e.g. here and here), but I need help getting the actual BlackBerry sample code to run (or perhaps somebody to tell me why this is doomed to not work!).

The BlackBerry website has some sample 'browser' code based on different API versions available:
V4.5 API sample
V5.0 API sample

I've found the sample code that ships with the Component Packs (more info here), and tried to get the V4.5 sample code to work. I was hoping this would be a useful starting point...

I've managed to get BrowserFieldDemo to compile in Eclipse and run in the Simulator (I needed to comment out the whole of BrowserContentManagerDemo.java otherwise that class would run instead).

Unfortunately, I just get a white screen in the Simulator. When I add in logging and use the debugger, it all seems to go wrong at the getBrowserContent() line here:

BrowserContent browserContent = null;

try
{
    browserContent = _renderingSession.getBrowserContent(connection, this, e);
    <snip>
}
catch (RenderingException re)
{
  EventLogger.logEvent(ID, (re + "").getBytes(), EventLogger.ERROR);
  System.err.println(re);
}

The Exception returned is:

net.rim.device.api.browser.field.RenderingException: IOException in connection

I've tried building and using the Simulator with the 4.5.0 and 4.7.0 Component Packs, but they both have the same symptoms.

If I push the samples.cod file to my device and start it, I get "Error starting samples: Module 'samples' attempts to access a secure API". Presumably I would need to sign the sample code with my code signing keys (which I do have), which I am not sure how to do in Eclipse.

So, my questions are:

1) Has anybody actually got this V4.5 sample code working? Should I give up on the Simulator and use the device instead?

2) Can this V4.5 approach work for displaying some simple HTML data that I have? e.g. can I use a localhost URL, or perhaps create a custom HttpConnection to serve up the data?

I need to support BlackBerry models running V4.5, V4.7 and V5.0, if at all possible.

Any tips would be appreciated!

+1  A: 

Make sure that you launch the MDS simulator before launching the device simulator. All or most of the samples that use HTTP don't specify a transport and so will use the default MDS transport, which means if you don't have MDS simulator running then it won't be able to make an HTTP connection.

Marc Novakowski
Thanks - that got the sample code working! I needed to run "run.bat" from inside this Component Pack directory: net.rim.eide.componentpack4.5.0_4.5.0.16\components\MDS. Apparently there is a way to ensure this happens each time the normal Simulator is launched too.
Dan J
I'm accepting this answer because getting it to work in the Simulator was the main thing blocking me. Thanks to coldice for help with my general approach.
Dan J
+2  A: 

You should implement you're own HttpConnection which will take String argument in constructor and return all values like getType(), getLength(), InputStream on openInputStream(), etc. Then use it with browser field, just like in sdk BrowserFieldDemo.

public class HttpConnectionImpl implements HttpConnection {
    private long streamLength = 7000;
    private DataInputStream dataInput;
    private InputStream in;
    private String encoding = "text/html";

    public HttpConnectionImpl(String data) {
        try {
            in = new ByteArrayInputStream(data.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());
    }
}

See full code with example of use

Max Gontar
Thanks, good to know other people have looked at using this approach too!
Dan J
You're welcome!
Max Gontar
I've tested this by hacking the sample code, and it works in the Simulator. Haven't tried it on any actual devices yet though...
Dan J