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
Contributor
Posts: 19
Registered: ‎01-18-2011
My Device: Bold 9000
My Carrier: at&t
Accepted Solution

BrowserField can not display utf-8 characters issue

I parse the xml feeds using DefaultHandler, then put the titles in the ListField and clicks each title goes to the detail view with the detail html. 

 

For ListField, the title in Japanese can be displayed well( I am using Graphics.drawText for it), but in BrowserField, no lucky, Here is the code, in mainscreen's constructor:

 

 

        BrowserFieldConfig config = new BrowserFieldConfig();
        
        HttpHeaders headers = new HttpHeaders();
        headers.addProperty(HttpHeaders.HEADER_CONTENT_TYPE, HttpHeaders.CONTENT_TYPE_TEXT_HTML);
        headers.addProperty(HttpHeaders.HEADER_ACCEPT_CHARSET, "UTF-8");
        
        config.setProperty(BrowserFieldConfig.HTTP_HEADERS, headers);
        __bf = new BrowserField(config);
        ProtocolController controller = new ProtocolController(__bf);
        config.setProperty(BrowserFieldConfig.CONTROLLER, controller);
        controller.setNavigationRequestHandler("http", new BrowserFieldNavigationRequestHandler() {
            public void handleNavigation(BrowserFieldRequest request) throws Exception {
                Logger.debug("handleNavigation " + request.getURL());
                BrowserSession bs = BrowserSessionFactory.getBroswerSession();
                bs.displayPage(request.getURL());
            }
        });
        controller.setResourceRequestHandler("http", new BrowserFieldResourceRequestHandler() {
            public InputConnection handleResource(BrowserFieldRequest request) throws Exception {
                if (request.getURL().startsWith("local:///")) {
                    return new BrowserFieldResponse(request.getURL(), ArticleScreen.class.getResourceAsStream(request
                            .getURL().substring("local:///".length())), "image/png");
                }
                return (InputConnection) Connector.open(request.getURL() + BaseFeedParser.connSuffix);
            }
        });

        __bf.displayContent(content(title, meta), "");
        add(__bf);

 

    and the content()

 

private String content(String title, String meta) {
       String html = "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n";
        html += "  <head>\n";
        html += "    <meta http-equiv=\"content-type\" content=\"text/html;\" />\n";
        html += "    <style type=\"text/css\">\n";
        html += "      body {\n";
        html += "        background-color: #fff;\n";
        html += "        font-family: helvetica;\n";
        html += "        font-size: 11pt;\n";
        html += "        color: black;\n";
        html += "        margin: 0 10px 0 10px;\n";
        html += "        padding: 10px 10px 56px 6px;\n";
        html += "      }\n";
        html += "      a {\n";
        html += "        color: red;\n";
        html += "      }\n";
        html += "      a.blippr-inline-smiley {\n";
        html += "        display: none;\n";
        html += "      }\n";
        html += "      img {\n";
        html += "        width: 280px; max-width: 280px; margin-top: 10px; height:auto; \n";
        html += "        margin-bottom: 20px; clear:both; text-align:left;\n";
        html += "      }\n";
        html += "      div.ad {\n";
        html += "        margin-left: -16px;\n";
        html += "        margin-bottom: 10px;\n";
        html += "      }\n";
        html += "      h1 {\n";
        html += "        font-size: 14pt;\n";
        html += "        margin: 0px;\n";
        html += "        padding: 0px;\n";
        html += "      }\n";
        html += "      h2 {\n";
        html += "        font-size: 13pt;\n";
        html += "        margin: 0px;\n";
        html += "        padding: 0px;\n";
        html += "      }\n";
        html += "      h3 {\n";
        html += "        font-size: 11pt;\n";
        html += "        font-weight: normal;\n";
        html += "        color: #555;\n";
        html += "        padding: 0px;\n";
        html += "        margin: 10px 0px;\n";
        html += "      }\n";
        html += "    </style>\n";
        html += "  </head>\n";
        html += "  <body>\n";
       html += "    <h1>" + title + "</h1><h3>" + meta
                + "</h3>";
        html += "  </body>";
        html += "</html>";
      return html;
}

 

   Change the 

     <meta http-equiv=\"content-type\" content=\"text/html;\" />\n"

 

to 

 

 <meta http-equiv=\"content-type\" content=\"text/html; charset:utf-8\" />\n"

 

will not work too.

 

Any one has any idea?

 

Appreciate that.

 

 

 

 

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

Re: BrowserField can not display utf-8 characters issue

If you are seeing black squares in place of the characters, then this is almost certainly a font issue.  Try removing the font specification from your css and see if it displays. 

 

To make this test easier, you should be able to put an html file on your SD Card and display this in the Browser - it should show the same issue.  To display an html file

a) Start the media application

b) Use the menu button and select the Explorer menu option

c) explore down the directories to find your file and click on it to display it in the Browser. 

 

Having done this, you can simply put html files on your SD Card to test if the characters are displayed. 

Contributor
Posts: 19
Registered: ‎01-18-2011
My Device: Bold 9000
My Carrier: at&t

Re: BrowserField can not display utf-8 characters issue

Thanks, peter

 

I saw not black, but ‘????????' in the BrowserField,

 

and I save the content to the html file, and open it in media, it is ???????? too. here is code to save it to file.

 

FileConnection fconn = null;
        try {
            fconn = (FileConnection) Connector.open("file:///store/" + title + ".html");
            if (!fconn.exists()) {
                fconn.create();
            }
            fconn.setReadable(true);
            fconn.setWritable(true);
            OutputStream os = fconn.openOutputStream();
            os.write(html.getBytes());
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fconn!=null) {
                try {
                    fconn.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

 

  After I remove all the css, still got ??????.

 

  while I create a LabelField to hold the content, No ????? and these characters display well.

 

  Do you think where could the problem be?

  Content is like 'http://blogs.oracle.com/readingLists/oracleblogs.xml'

 

 

 

 

Highlighted
Regular Contributor
Posts: 69
Registered: ‎10-19-2010
My Device: BB8520

Re: BrowserField can not display utf-8 characters issue

Hi, I have the same problem with you but I do this to solve the problem

 

I implement httpconnection class and implement this method

 

 

public String getHeaderField(String name) throws IOException {
        String result;
        if(name.equalsIgnoreCase("content-type")) {
                result = getType();
        }
        else if(name.equalsIgnoreCase("content-length")) {
                result = Long.toString(getLength());
        }
    else if(name.equalsIgnoreCase("content-location")) {
                result = getURL();
        }
        else if(name.equalsIgnoreCase("accept-charset")) {
                result = "UTF-8";
        }
        else {
            result = null;
        }
        return result;

}

 Try it out.

 

Contributor
Posts: 19
Registered: ‎01-18-2011
My Device: Bold 9000
My Carrier: at&t

Re: BrowserField can not display utf-8 characters issue

Thanks, gillaraz,  that works!!

 

Visitor
Posts: 1
Registered: ‎10-31-2011
My Device: Blackberry 9800
My Carrier: Vodafone

Re: BrowserField can not display utf-8 characters issue

Hi can you provide me with the example code of how to work with httpconnection and browsefield.

I am stuck with the same issue of UTF-8 charactors.

 

Thanks