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
Developer
mnpaslay
Posts: 105
Registered: ‎03-09-2009
My Device: Not Specified
Accepted Solution

http demo question

[ Edited ]

Is the httpdemo the most efficient way to get a http doc into a string?  It seems like there should be a much simpler way...

 

for an example, I'm trying to get http://www.corocks.com/bookstore.xml into a string...

 

I feel like I should be able to do this by just creating an input string then turning that into a string:

 

 

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

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

try{
                conn=(StreamConnection)Connector.open("http://www.corocks.com/bookstore.xml");
           

catch (Exception e){
                System.out.println(e.toString());
            }
        }
}

 

and, will i'm at in, where does println info show up?  not on the blackberry screen...

Message Edited by mnpaslay on 03-29-2009 03:23 PM
Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: http demo question

I presume that you want to issue an http request to the URL you have specified (which doesn't exit) and read what would be displayed on the screen, if you did it in a Browser?

 

If so, there is no simple way.

 

Sorry I'm familiar with the JDE and not the Eclipse, but I believe that in there also, the System.out.println(..) output will appear in the debugger output window when you run the Simulator from your IDE. 

Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: http demo question

In the Eclipse plugin, simulator output appears in the BlackBerry Simulator Output Console in the Console view, but only when you debug. If you invoke run, System.out is connected (as far as I can tell) to the Great Bit Bucket in the sky.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Developer
mnpaslay
Posts: 105
Registered: ‎03-09-2009
My Device: Not Specified

Re: http demo question

[ Edited ]

This url does exist:

 

http://www.corocks.com/bookstore.xml

 

I just put the demoxml file up, I'm trying to merge the httptest demo and xmldemo to understand how to pull and parse an xml files from an http address....and it is giving me the worst headache!!

 

In the end, I want to put the data in the xml file into string arrays for display on the blackberry screen...

 

Message Edited by mnpaslay on 03-29-2009 03:21 PM
Message Edited by mnpaslay on 03-29-2009 03:22 PM
Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: http demo question

So have we answered your questions now?
Please use plain text.
Developer
mnpaslay
Posts: 105
Registered: ‎03-09-2009
My Device: Not Specified

Re: http demo question

[ Edited ]

No, I'm using the sample code from:

 

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800599/How_To_...

 

just with the address changed and it does not work.  I'm not sure why...

 

 

 

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="Title"; _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.corocks.com/bookstore.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){ System.out.println(e.toString()); } }//end connection function }// end connection class }//end XML_Parsing_Sample

 

Message Edited by mnpaslay on 03-29-2009 10:00 PM
Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: http demo question

I assume you're trying this in a device simulator. When you say "it does not work", what doesn't work? If it's just hanging, step through with the debugger and tell us what line is hanging. But first make sure an MDS simulator is running. If you're getting an exception, what is it?



Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Developer
mnpaslay
Posts: 105
Registered: ‎03-09-2009
My Device: Not Specified

Re: http demo question

[ Edited ]

It is just hanging.  I do have MDS simulator on;however, the network emulation seems flaky.  I will check it after an attempt using the browser and sometimes it connects and sometimes it does not.  It think I figured out some of these issues, I had the simulor set to default instead of 9530, it stopped the breakpoints and since I was starting it from different workspaces, it followed the settings around....

 

the last line of code is(has arrow on it, but continue does nothing:

 

           doc = docBuilder.parse(conn.openInputStream());

 

and, inthe debug window I see

 

org.xml.sax.SAXParseException: End tag does not match start tag 'meta'.

 

however, that xml file is the sample file and has all end tags...

Message Edited by mnpaslay on 03-29-2009 11:58 PM
Please use plain text.
Developer
mnpaslay
Posts: 105
Registered: ‎03-09-2009
My Device: Not Specified

Re: http demo question

Please help!

 

Now i'm getting an exception;

 

org.xml.sax.SAXParseException: Expecting an element.

 

i do not know why!  All I want to do is parse this example file at http://www.corocks.com/bookstore.xml before i pull all my hair out....

Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: http demo question

[ Edited ]

Well, the error message is revealing. The example file you say you are retrieving does not have a 'meta' tag. But the stream being parsed evidently does (the parser thinks the whole thing is a 'meta' tag). You aren't retrieving what you think you are retrieving.

 

First, I would separate creating of the input stream from the call to parse:

 

InputStream is = conn.openInputStream();
if (is != null) {
doc = docBuilder.parse(is);
}

Then, you can put a break point at the 'if' statement and look at the data in the input stream. That will tell you what the parser is actually trying to do. You can also then distinguish between the connection hanging and the parser hanging, which should provide additional clues.

 

It is likely that your application isn't "hanging", it's "crashing" (are you catching the exceptions?)  and hence unresponsive.

Message Edited by Ted_Hopp on 04-02-2009 03:15 PM



Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.