09-08-2012 01:44 AM - edited 09-08-2012 01:57 AM
hi,
i developed Rss Reader Example,my requirement is to fetch TITLE AND LINK values from Rss xml File and display it on my output console,i have used System.out.println,i am not able to retrieve the title and link values from Rss File,can any one help me what should i make changes in my code?
here my code:
public class RssMain extends UiApplication {
RssMain theApp;
public static void main(String[] args)
{
// create an instance of our app
RssMain theApp = new RssMain();
// "run" the app
theApp.enterEventDispatcher();
}
// app constructor
public RssMain()
{
// create an instance of the main screen of our application
RssScreen screen = new RssScreen();
// make the screen visible
UiApplication.getUiApplication().pushScreen(screen
}
class RssScreen extends MainScreen{
public RssScreen(){
String rssUrl = "http://twitter.com/statuses/user_timeline/27756405
String[][] urlData = RSSHandler.getURLFromRSS(rssUrl);
for (int i = 0; i < urlData.length; i++) {
String title = urlData[0][i];
String url = urlData[1][i];
System.out.println("TITLE "+title);//i want to print on my console
System.out.println("URL "+url);
}
}
}
}
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import net.rim.blackberry.api.browser.Browser;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.util.Arrays;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class RSSHandler extends DefaultHandler {
boolean isItem = false;
boolean isTitle = false;
boolean isLink = false;
String[] title = new String[] {};
String[] link = new String[] {};
String value = "";
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
if (!isItem) {
if (name.equalsIgnoreCase("item"))
isItem = true;
} else {
if (name.equalsIgnoreCase("title"))
isTitle = true;
if (name.equalsIgnoreCase("link"))
isLink = true;
}
}
public void characters(char[] ch, int start, int length)
throws SAXException {
if (isTitle || isLink)
value = value.concat(new String(ch, start, length));
}
public void endElement(String uri, String localName, String name)
throws SAXException {
if (isItem && name.equalsIgnoreCase("item"))
isItem = false;
if (isTitle && name.equalsIgnoreCase("title")) {
isTitle = false;
Arrays.add(title, value);
value = "";
}
if (isLink && name.equalsIgnoreCase("link")) {
isLink = false;
Arrays.add(link, value);
value = "";
}
}
public static String[][] getURLFromRSS(String url) {
InputStream is = null;
HttpConnection connection = null;
RSSHandler rssHandler = new RSSHandler();
try {
connection = (HttpConnection) Connector.open(url);
is = connection.openInputStream();
try {
SAXParser parser = SAXParserFactory.newInstance()
.newSAXParser();
parser.parse(is, rssHandler);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
if (connection != null)
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String[][] result = new String[2][];
result[0] = rssHandler.title;
result[1] = rssHandler.link;
return result;
}
}
09-08-2012 04:46 AM
If you are using eclipse plug-in for development than you need to run your program in debug mode to see the logs/SOP. Choose debug as option from the eclipse.
09-10-2012 03:42 AM
09-10-2012 04:12 AM