开发者

Blackberry XML Parsing Application not working

I found one sample application from the Blackberry knowledgebase.

From that application I have put that sample application on my eclipse plugin, and the code is as follows :

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 wh开发者_如何学JAVAich 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

But when I am running this application, the simulator just showing me a Blank screen with label Requesting...

Anybody help me out for this ?

Thanks in advance...


Try this

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

This synchronized (UiApplication.getEventLock()) is really important, you need this every time thread try to access the UI.

More solutions exist, see the documentation

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜