Blackberry XML解析不起作用

时间:2013-05-23 08:41:47

标签: blackberry java-me xml-parsing

我有以下代码用于解析

public 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) {

        synchronized (UiApplication.getEventLock()) {
            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{

                conn=(StreamConnection)Connector.open
                  ("http://www.islamicfinder.org/prayer_service.php?" +
                        "country=united_arab_emirates&city=abu_dhabi&state=01&zipcode=&latitude" +
                        "=24.4667&longitude=54.3667&timezone=4&HanfiShafi=1&pmethod=4&fajrTwilight1=" +
                        "10&fajrTwilight2=10&ishaTwilight=10&ishaInterval=30&dhuhrInterval=1&" +
                        "maghribInterval=1&dayLight=0&simpleFormat=xml&monthly=1&month=");

                _screen.add(new RichTextField("connn---"+conn));
                DocumentBuilderFactory docBuilderFactory
                  = DocumentBuilderFactory. newInstance(); 
                DocumentBuilder docBuilder
                  = docBuilderFactory.newDocumentBuilder();
                docBuilder.isValidating();
                doc = docBuilder.parse(conn.openInputStream());
                doc.getDocumentElement ().normalize ();
                NodeList list=doc.getElementsByTagName("prayer");
                _node=new String();
                _element = new String();


                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);
                }
            }
            catch (Exception e){
                System.out.println(e.toString());
            }
        }
    }
}

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

有人可以帮我这样做吗?我正在使用bb9900模拟器。

1 个答案:

答案 0 :(得分:1)

我看到了几个问题:

更改后台线程上的UI

不允许您从后台线程直接修改用户界面(UI)。这包括向Field添加Screen个对象。但是,在您的Connection#run()方法中,您可以执行此操作:

 _screen.add(new RichTextField("connn---"+conn));

该行可能会抛出异常:

  

java.lang.IllegalStateException:在不保存事件锁的情况下访问UI引擎。

并输入您的catch处理程序。它应该打印出一条消息,但您可能没有在Eclipse 控制台窗口中注意到它。

我要将该行移出run()方法,然后将其添加到屏幕的构造函数中。或者,你可以这样做一个安全的方法:

public void addTextField(final String message) {
   UiApplication.getUiApplication().invokeLater(new Runnable() {
      public void run() {
          // code in here is run on UI thread, and can safely change the UI
          _screen.add(new RichTextField("connn---"+message));
      }
   });
}

然后,从Connection#run()内部,将其称为:

addTextField(conn.toString());

现在,这仍然可能不是你想要的。打印出字符串化的Connection对象并不太有用。也许您想要打印URL而不是?无论如何,我会让你决定。

网址请求缺少参数

在我看来,您错过了网址末尾的参数:

"http://www.islamicfinder.org/prayer_service.php?" +
  "country=united_arab_emirates&city=abu_dhabi&state=01&zipcode=&latitude" +
  "=24.4667&longitude=54.3667&timezone=4&HanfiShafi=1&pmethod=4&fajrTwilight1=" +
  "10&fajrTwilight2=10&ishaTwilight=10&ishaInterval=30&dhuhrInterval=1&" +
  "maghribInterval=1&dayLight=0&simpleFormat=xml&monthly=1&month="

因此,也许您的服务器也需要将月份附加到此URL的末尾。

尝试这两件事。这应该让你更接近。