Selasa, 22 Mei 2012

Membuat RSS Reader dengan J2ME NetBeans


RSS Reader dengan J2ME
Untuk membuat aplikasi RSS Reader di Java ME NetBeans dapat digunakan library KXML. Langkah-langkah pembuatannya sebagai berikut :

1. Buka Netbeans. Klik menu File-New Project. Pilih Categories : Java ME, Projects : Mobile Application. Klik Next. Pada dialog berikutnya, isikan Project Name : MobileRSSReader. Kemudian cek Set As Main Project dan hilangkan cek pada Create Hello Midlet. Klik Next. Klik Next. Klik Finish.


2. Pada Project TreeView klik kanan, pilih New – Visual Midlet. Kemudian akan muncul dialog, isikan Class Name : MNews, package : my.apprsseader. Klik Finish. Akan ditampilkan MIDlet kosong pada Design. Dari Palette Displayables tambahkan Form, kemudian ubah namanya menjadi FMain. Kemudian pada Form FMain tambahkan itemCommand dan exitCommand dari Pallete Commands. Ubah name dari itemCommand menjadi cmGet dan label-nya menjadi “Get”. Setelah itu tarik garis dari MIDlet Started menuju FMain, kemudian dari FMain exitCommand tarik garis ke MIDlet Resumed..


3. Pada Screen Design akan ditampilkan Design dari FMain. Ubah title dari FMain menjadi “New Reader”, kemudian tambahkan 1 buah TextField. Ubah name-nya menjadi tList. Kemudian ubah property label dari TextField tersebut menjadi “News List”. Kemudian property text-nya kosongkan, menjadi “”. Sehingga tampak seperti pada tampilan berikut ini :


4. Pada cmGet, klik kanan pilih Go to Source. Pada Source akan muncul kode program dari method public void commandAction(), di dalamnya pada blok if (command == cmGet) { } tambahkan kode program berikut :

    public void commandAction(Command command, Displayable displayable) {
        // write pre-action user code here
        if (displayable == FMain) {
            if (command == cmGet) {
                // write pre-action user code here
                try {
                    new Thread(new Runnable()
                    {
                        public void run()
                        {
                            try {
                                HttpConnection conn;
                                DataInputStream dis;
                                DataOutputStream dos;

                                conn = (HttpConnection) Connector.open("http://www.nytimes.com/services/xml/rss/nyt/National.xml");
                                conn.setRequestMethod(HttpConnection.GET);
                                conn.setRequestProperty("Content-Type", "text/xml");
                                dos = new DataOutputStream(conn.openDataOutputStream());

                                dis = new DataInputStream(conn.openDataInputStream());
                                if (conn.getResponseCode() == HttpConnection.HTTP_OK) {
                                    doParse(dis);
                                    String newsList = "";
                                    for (int i = 0; i < jumlah; i++) {
                                        newsList = newsList + sTitle + "\n";
                                        newsList = newsList + sLink + "\n";
                                        newsList = newsList + sDescription + "\n";
                                        newsList = newsList + "--------------------------\n";
                                    }
                                } else {
                                    tList.setString("Koneksi Error");
                                }
                            } catch (Exception e) {
                            }
                        }
                    }).start();
                } catch (Exception e) {
                }

                // write post-action user code here
            } else if (command == exitCommand) {
                // write pre-action user code here
                exitMIDlet();
                // write post-action user code here
            }
        }
        // write post-action user code here
    }

5. Lanjutkan untuk cmEdit, cmDelete, cmSave, cmCancel dan lainnya. Berikut ini kode program selengkapnya :

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;

import java.io.*;
import org.kxml2.io.*;
import org.xmlpull.v1.*;

public class MNews extends MIDlet implements CommandListener {

    private boolean midletPaused = false;

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">
    private Form FMain;
    private TextField tList;
    private KXmlParser parser;
    private int jumlah = 0;
    private String sTitle[] = new String[100];
    private String sLink[] = new String[100];
    private String sDescription[] = new String[100];
    public void commandAction(Command command, Displayable displayable) {
        // write pre-action user code here
        if (displayable == FMain) {
            if (command == cmGet) {
                // write pre-action user code here
                try {
                    new Thread(new Runnable()
                    {
                        public void run()
                        {
                            try {
                                HttpConnection conn;
                                DataInputStream dis;
                                DataOutputStream dos;

                                conn = (HttpConnection) Connector.open("http://www.nytimes.com/services/xml/rss/nyt/National.xml");
                                conn.setRequestMethod(HttpConnection.GET);
                                conn.setRequestProperty("Content-Type", "text/xml");
                                dos = new DataOutputStream(conn.openDataOutputStream());

                                dis = new DataInputStream(conn.openDataInputStream());
                                if (conn.getResponseCode() == HttpConnection.HTTP_OK) {
                                    doParse(dis);
                                    String newsList = "";
                                    for (int i = 0; i < jumlah; i++) {
                                        newsList = newsList + sTitle + "\n";
                                        newsList = newsList + sLink + "\n";
                                        newsList = newsList + sDescription + "\n";
                                        newsList = newsList + "--------------------------\n";
                                    }
                                } else {
                                    tList.setString("Koneksi Error");
                                }
                            } catch (Exception e) {
                            }
                        }
                    }).start();
                } catch (Exception e) {
                }

                // write post-action user code here
            } else if (command == exitCommand) {
                // write pre-action user code here
                exitMIDlet();
                // write post-action user code here
            }
        }
        // write post-action user code here
    }
    public void doParse(InputStream isxml) {
        parser = new KXmlParser();
        try {
            parser.setInput(new InputStreamReader(isxml));
            parser.next();
            parser.require(XmlPullParser.START_TAG, null, "rss");
            parser.nextTag();

            parser.require(XmlPullParser.START_TAG, null, "channel");

                while (parser.nextTag() != XmlPullParser.END_TAG) {
                    parser.require(XmlPullParser.START_TAG, null, "title");
                    sTitle[jumlah] = new String("Title: " + parser.nextText());
                    parser.require(XmlPullParser.END_TAG, null, "title");
                    parser.nextTag();

                    parser.require(XmlPullParser.START_TAG, null, "link");
                    sLink[jumlah] = new String("Link: " + parser.nextText());
                    parser.require(XmlPullParser.END_TAG, null, "link");
                    parser.nextTag();
                   
                    parser.require(XmlPullParser.START_TAG, null, "description");
                    sDescription[jumlah] = new String("Description: " + parser.nextText());
                    parser.require(XmlPullParser.END_TAG, null, "description");
                    parser.nextTag();
                }
                parser.require(XmlPullParser.END_TAG, null, "channel");
                jumlah++;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

6. Klik Run untuk menjalankan aplikasi. Kemudian klik menu Get, maka akan ditampilkan Berita (News) dalam bentuk RSS dari Web (misalnya web New York Times).