java - Blackberry XML 解析应用程序无法正常工作

标签 java xml blackberry blackberry-eclipse-plugin

我从 Blackberry 知识库中找到了一个示例应用程序。

从该应用程序中,我已将该示例应用程序放在我的 Eclipse 插件上,代码如下:

import javax.microedition.io.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import net.rim.device.api.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;

class XML_Parsing_Sample extends UiApplication {
    // creating a member variable for the MainScreen
    MainScreen _screen = new MainScreen();
    // string variables to store the values of the XML document
    String _node, _element;
    Connection _connectionthread;

    public static void main(String arg[]) {
        XML_Parsing_Sample application = new XML_Parsing_Sample();
        // create a new instance of the application
        // and start the application on the event thread
        application.enterEventDispatcher();
    }

    public XML_Parsing_Sample() {
        _screen.setTitle("XML Parsing");// setting title
        _screen.add(new RichTextField("Requesting....."));
        _screen.add(new SeparatorField());
        pushScreen(_screen); // creating a screen
        // creating a connection thread to run in the background
        _connectionthread = new Connection();
        _connectionthread.start();// starting the thread operation
    }

    public void updateField(String node, String element) {
        // receiving the parsed node and its value from the thread
        // and updating it here
        // so it can be displayed on the screen
        String title = "My App";
        _screen.add(new RichTextField(node + " : " + element));

        if (node.equals(title)) {
            _screen.add(new SeparatorField());
        }
    }

    private class Connection extends Thread {
        public Connection() {
            super();
        }

        public void run() {
            // define variables later used for parsing
            Document doc;
            StreamConnection conn;

            try {
                // providing the location of the XML file,
                // your address might be different
                conn = (StreamConnection) Connector
                        .open("http://www.sufalamtech.com/demo/moviewebservice/Test.xml");
                // next few lines creates variables to open a
                // stream, parse it, collect XML data and
                // extract the data which is required.
                // In this case they are elements,
                // node and the values of an element
                DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
                        .newInstance();
                DocumentBuilder docBuilder = docBuilderFactory
                        .newDocumentBuilder();
                docBuilder.isValidating();
                doc = docBuilder.parse(conn.openInputStream());
                doc.getDocumentElement().normalize();
                NodeList list = doc.getElementsByTagName("*");
                _node = new String();
                _element = new String();
                // this "for" loop is used to parse through the
                // XML document and extract all elements and their
                // value, so they can be displayed on the device

                for (int i = 0; i < list.getLength(); i++) {
                    Node value = list.item(i).getChildNodes().item(0);
                    _node = list.item(i).getNodeName();
                    _element = value.getNodeValue();
                    updateField(_node, _element);
                }// end for
            }// end try
            // will catch any exception thrown by the XML parser
            catch (Exception e) {
                Dialog.alert("exception = " + e);
            }
        }// end connection function
    }// end connection class
}// end XML_Parsing_Sample

但是当我运行这个应用程序时,模拟器只是向我显示一个带有标签请求...的空白屏幕

有人帮我解决这个问题吗?

提前致谢...

最佳答案

试试这个

public void updateField(String node, String element) {
    // receiving the parsed node and its value from the thread
    // and updating it here
    // so it can be displayed on the screen
    //Don't forget to add this next line when called from a thread
    synchronized (UiApplication.getEventLock()) {
        String title = "My App";
        _screen.add(new RichTextField(node + " : " + element));

        if (node.equals(title)) {
            _screen.add(new SeparatorField());
        }
        }
}

这个同步(UiApplication.getEventLock())非常重要,每次线程尝试访问 UI 时都需要它。

存在更多解决方案,请参阅documentation

关于java - Blackberry XML 解析应用程序无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2989449/

相关文章:

XML 回车符在发布后消失了

java - 在 BlackBerry sim 上使用 ";deviceside=true"执行 browserfield 时出现问题

java - 如何使用 Java 使用现有的 dtd 生成数据示例的 xml 文件

java - 读取自定义 JAD 属性时出现空值

android - 需要下载/安装 wifi 之前的最大移动应用程序大小

java - 圣诞树的错误展示

java - 我在哪里可以找到一个很好的 HTMLEditorKit 教程/引用,它实际上解释了如何编辑 HTML 文档?

java - Selenium FirefoxDriver 在动画发生时点击了错误的链接

java - Spring Roo 命令执行包错误

javascript - 来自 AutoCad 2011 的 XML 文件