BlackBerry Forums Support Community

BlackBerry Forums Support Community (http://www.blackberryforums.com/)
-   Developer Forum (http://www.blackberryforums.com/developer-forum/)
-   -   Using getDisplayableContent to display raw HTML (http://www.blackberryforums.com/developer-forum/203921-using-getdisplayablecontent-display-raw-html.html)

jj12 09-11-2009 09:52 AM

Using getDisplayableContent to display raw HTML
 
Hi. I've overloaded the HttpConnection class so that it returns a string instead of making a connection. But when I call getDisplayableContent(), the resulting field contains no data. I looked at the connection class and the data is in the class. Does anyone know what we need to do for getDisplayableContent() to display the content? I'm not sure what method it is calling from the connection class because I've tried breakpoitns in all the methods, but they don't get called. Thanks for any help.


Code:

renderingSession = RenderingSession.getNewInstance();
renderingSession.getRenderingOptions().setProperty(RenderingOptions.CORE_OPTIONS_GUID, RenderingOptions.SHOW_IMAGES_IN_HTML, false);
HttpConnection connection = new CustomHttpConn("This is a test.");
BrowserContent browserContent = _renderingSession.getBrowserContent(connection, null, null);
Field field = browserContent.getDisplayableContent();


kelumkps 09-18-2009 01:42 AM

You can implement javax.microedition.io.HttpConnection interface so that it takes your text like this.

Code:


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

public HttpConnectionImpl(String text) {
try {
in = new ByteArrayInputStream(text.getBytes("UTF-8"));
dataInput = new DataInputStream(in);
} catch (Exception e) {
System.out.println("HttpConnectionImpl : Exception : " + e);
}

}

// implement rest of the methods of HttpConnection interface with dummy methods


}


Then you can pass an object of this implementation to get the field as follows;

Code:


String text= "This is a test.";

HttpConnection connection = new HttpConnectionImpl(text);

renderingSession = RenderingSession.getNewInstance();

renderingSession.getRenderingOptions().setProperty(RenderingOptions.CORE_OPTIONS_GUID, RenderingOptions.SHOW_IMAGES_IN_HTML, false);

BrowserContent browserContent = renderingSession.getBrowserContent(connection, null, null);

Field field = browserContent.getDisplayableContent();



Regards,

-Kelum-

jj12 09-20-2009 07:49 PM

Thank you for your help. But it seems nothing is being displayed after implementing your code. I must be doing something incorrect. Here's my connection class.

Code:

public class LocalHttpConn implements HttpConnection {
        private InputStream is = null;

        public LocalHttpConn(String html){
                try
                {
                        is = new ByteArrayInputStream(html.getBytes("UTF-8"));
                }
                catch(Exception e) {}
        }
       
        public LocalHttpConn(byte[] bits){
                is = new ByteArrayInputStream(bits);
        }
       
        public long getDate() throws IOException {
                return 0;
        }

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

        public String getFile() {
                return null;
        }

        public String getHeaderField(String name) throws IOException {
                return null;
        }

        public String getHeaderField(int n) throws IOException {
                return null;
        }

        public long getHeaderFieldDate(String name, long def)
                        throws IOException {
                return 0;
        }

        public int getHeaderFieldInt(String name, int def) throws IOException {
                return 0;
        }

        public String getHeaderFieldKey(int n) throws IOException {
                return null;
        }

        public String getHost() {
                return null;
        }

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

        public int getPort() {
                return 0;
        }

        public String getProtocol() {
                return null;
        }

        public String getQuery() {
                return null;
        }

        public String getRef() {
                return null;
        }

        public String getRequestMethod() {
                return null;
        }

        public String getRequestProperty(String key) {
                return null;
        }

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

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

        public String getURL() {
                return "";
        }

        public void setRequestMethod(String method) throws IOException {
                return;
        }

        public void setRequestProperty(String key, String value)
                        throws IOException {
                return;
        }

        public String getEncoding() {
                return "UTF-8";
        }

        public long getLength() {
                try {
                        if (is != null )
                                return is.available();
                        else return 0;
                } catch (IOException e) {
                }
                return 0L;
        }

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

        public DataInputStream openDataInputStream() throws IOException {
                return new DataInputStream(new ByteArrayInputStream(html.getBytes("UTF-8")));
        }

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

        public void close() throws IOException {
                is.close();
                return;
        }

        public DataOutputStream openDataOutputStream() throws IOException {
                return null;
        }

        public OutputStream openOutputStream() throws IOException {
                return null;
        }


This is the main code

Code:

                try
                {
                        _renderingSession = RenderingSession.getNewInstance();               
                        HttpConnection connection1 = new LocalHttpConn("<html>test is a test</html>");
                        BrowserContent browserContent = _renderingSession.getBrowserContent(connection1, null, null);
                       
                        Field field = browserContent.getDisplayableContent();               
                        tab1Manager.add(field);
                }

There are no errors when I run it, but the browser displays nothing and I don't get any errors. Thanks for any help.

kelumkps 09-21-2009 12:22 AM

I think the reason is implementation of the rest of the methods of javax.microedition.io.HttpConnection interface. Don't implement those methods to return null values. You can see below how I implemented it.

Code:


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


Try this way and hope it'll work for you.

Cheers,

-Kelum-

mdwheaton 09-28-2009 10:33 AM

I'm having the same problem as JJ
I've posted in the BlackBerry (RIM) forums as well.
I feel stupid at this point that this isn't working.
Did you get this working JJ?
If so, could you please post your Screen code, and the HttpConnectionImpl classes?
This is driving me insane that I can't get this working!!!


All times are GMT -5. The time now is 11:58 PM.

Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.