Saturday 13 October 2012

RssFeed Reader for BlackBerry Application

Nowadays Most of the Web Sites are using RssFeeds for their Content,So It is Important to know that  How to read and Parse an Rss File.
This application is designed to read and Parse  an RssFile(Input is RssFile) and Finally it displays a title and an Image on the Blackberry Screen....

The Project Package Structure on Eclipse :


You can check the Complete Code Here:

The Main Class:
import net.rim.device.api.ui.UiApplication;
public class RssApp extends UiApplication {
private static RssApp app;
public RssScreen screen;
public static void main(String args[]) {
app = new RssApp();
app.enterEventDispatcher();
}
//Constructor
public RssApp() {
screen = new RssScreen();
pushScreen(screen);
}
 }

RssScreen:
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.xml.parsers.DocumentBuilder;
import net.rim.device.api.xml.parsers.DocumentBuilderFactory;
      public class RssScreen extends MainScreen implements ListFieldCallback, FieldChangeListener {
private ListField _list;
private VerticalFieldManager mainManager;
private VerticalFieldManager subManager;
public long mycolor;
private Vector listElements = new Vector();
private Connection _connectionthread;
private Vector listLink;
private Vector listImage;
private String link;
public RssScreen() {
listLink = new Vector();
listImage = new Vector();
          //Background Image
final Bitmap backgroundBitmap = Bitmap
.getBitmapResource("blackbackground.png");
mainManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL
| Manager.NO_VERTICAL_SCROLLBAR) {
public void paint(Graphics graphics) {
graphics.drawBitmap(0, 0, Display.getWidth(),
Display.getHeight(), backgroundBitmap, 0, 0);
super.paint(graphics);
}
};
subManager = new VerticalFieldManager(Manager.VERTICAL_SCROLL
| Manager.VERTICAL_SCROLLBAR) {
protected void sublayout(int maxWidth, int maxHeight) {
int displayWidth = Display.getWidth();
int displayHeight = Display.getHeight();
super.sublayout(displayWidth, displayHeight);
setExtent(displayWidth, displayHeight);
}
};
add(mainManager);
_list = new ListField()
{
protected boolean navigationClick(int status, int time) {
return true;
}
public void paint(Graphics graphics)
{
graphics.setColor((int) mycolor);
super.paint(graphics);
}
};
mycolor = 0x00FFFFFF;
_list.invalidate();
_list.setEmptyString("* Feeds Not Available *", DrawStyle.HCENTER);
_list.setRowHeight(70);
_list.setCallback(this);
mainManager.add(subManager);
listElements.removeAllElements();
_connectionthread = new Connection();
_connectionthread.start();
}
          /*RssFileReading in a Seperate Thread*/
private class Connection extends Thread {
public Connection() {
super();
}
public void run() {
Document doc;
StreamConnection conn = null;
InputStream is = null;
try {
conn = (StreamConnection) Connector .open("your Rss Input File.xml"
+ ";deviceside=true");
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
docBuilderFactory.setIgnoringElementContentWhitespace(true);
docBuilderFactory.setCoalescing(true);
DocumentBuilder docBuilder = docBuilderFactory
.newDocumentBuilder();
docBuilder.isValidating();
is = conn.openInputStream();
doc = docBuilder.parse(is);
doc.getDocumentElement().normalize();
NodeList listImg = doc.getElementsByTagName("title");
for (int i = 0; i < listImg.getLength(); i++) {
Node textNode = listImg.item(i).getFirstChild();
listElements.addElement(textNode.getNodeValue());
}
NodeList list = doc.getElementsByTagName("image");
for (int a = 0; a < list.getLength(); a++) {
Node textNode1 = list.item(a).getFirstChild();
String imageurl = textNode1.getNodeValue();
Bitmap image = GetImage.connectServerForImage(imageurl
.trim() + ";deviceside=true");
listImage.addElement(image);
}
/*NodeList listlink = doc.getElementsByTagName("link");
for (int j = 0; j < listlink.getLength(); j++) {
Node textNode1 = listlink.item(j).getFirstChild();
link = textNode1.getNodeValue();
listLink.addElement(link);
}*/ 
} catch (Exception e) {
System.out.println(e.toString());
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ignored) {
}
}
if (conn != null) {
try {
conn.close();
} catch (IOException ignored) {
}
}
}
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
_list.setSize(listElements.size());
subManager.add(_list);
invalidate();
}
});
} }
/*This Method is Invoked for Each title on RssFile*/
public void drawListRow(ListField list, Graphics g, int index, int y,
int width) {
String title = (String) listElements.elementAt(index);
Bitmap image = (Bitmap) listImage.elementAt(index);
try {
int LEFT_OFFSET = 2;
int TOP_OFFSET = 8;
int xpos = LEFT_OFFSET;
int ypos = TOP_OFFSET + y;
int w = image.getWidth();
int h = image.getHeight();
g.drawBitmap(xpos, ypos, w, h, image, 0, 0);
xpos = w + 20;
g.drawText(title, xpos, ypos);
} catch (Exception e) {
e.printStackTrace();
}
}
public Object get(ListField list, int index) {
return listElements.elementAt(index);
}
public int indexOfList(ListField list, String prefix, int string) {
return listElements.indexOf(prefix, string);
}
public int getPreferredWidth(ListField list) {
return Display.getWidth();
}
public void insert(String toInsert, int index) {
listElements.addElement(toInsert);
}
public void fieldChanged(Field field, int context) {
}
}

UtilClass: This Class will return a Bitmap reference

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import net.rim.device.api.io.IOUtilities;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.EncodedImage;

public class GetImage {
public static Bitmap connectServerForImage(String url) {
HttpConnection httpConnection = null;
DataOutputStream httpDataOutput = null;
InputStream httpInput = null;
int rc;
Bitmap bitmp = null;
try {
httpConnection = (HttpConnection) Connector.open(url);
rc = httpConnection.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
throw new IOException("HTTP response code: " + rc);
}
httpInput = httpConnection.openInputStream();
InputStream inp = httpInput;
byte[] b = IOUtilities.streamToBytes(inp);
EncodedImage hai = EncodedImage.createEncodedImage(b, 0, b.length);
return hai.getBitmap();
} catch (Exception ex) {
// System.out.println("URL Bitmap Error........" + ex.getMessage());
} finally {
try {
if (httpInput != null)
httpInput.close();
if (httpDataOutput != null)
httpDataOutput.close();
if (httpConnection != null)
httpConnection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return bitmp;
}
  }

Thanks..............









6 comments:

  1. Wonderful website. i like. very informational. please continue to post many articles on mobile development, it is very useful. You solution helped me with a very long problem i was having. thank you. looking forward for your next article solution.

    one thing to fix though u have a missing comma in this line of code public class RssScreen extends MainScreen implements ListFieldCallback FieldChangeListener between ListFieldCallback and FieldChangeListener. it should be RssScreen extends MainScreen implements ListFieldCallback, FieldChangeListener

    thank you.

    ReplyDelete
  2. Thanks so much for your Encouragement.........I will continue to Post many articles

    ReplyDelete
  3. Nice article... very helpful. however your 'main class' is pushing TitleImageScreen that does not exist in the codes you provided.

    ReplyDelete
  4. @Baldor Oloruntoba Thanks for commenting,Now I just modified it again.....

    ReplyDelete
  5. hai, can u give me xml sample for this code..

    thanks

    ReplyDelete
    Replies
    1. i got sample from w3schools, and its work.

      how if i want to where image click, it show medium image size with title ?

      thanks for your help

      Delete