⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rssreader.java

📁 基于J2ME的RSS阅读器
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package pp.rss.reader.midlet;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import java.io.*;import java.util.*;import javax.microedition.rms.*;import pp.rss.reader.ui.*;import pp.rss.reader.model.RSSSourceEntry;/** * @author Administrator */public class RSSReader extends MIDlet implements CommandListener{    protected Display display;    public List lsRSSSource;    private Command cmdExit;    private Command cmdAdd;    private Command cmdEdit;    private Command cmdDelete;    public static final int MAX_RSS_SOURCE=15;    public static final int MAX_RSS_LINK=75;    public static final String[] DEFAULT_RSS_DESCRIPTIONS=    {"ECommerce Times","Humorous Quotes",     "Book Reviews","Slashdot.org","Web Developer"};    public static final String[] DEFAULT_RSS_LINKS=    {        "http://www.ecommercetimes.com/perl/syndication/rssfull.pl",        "http://www.quotationspage.com/data/qotd.rss",        "http://p.moreover.com/cgi-local/page?index_bookreviews.rss",        "http://slashdot.org/slashdot.rss",        "http://headlines.internet.com/internetnews/wd-news/news.rss"    };    public Vector vecRSSSource;    private RecordStore rsRSSSource;    private static final String REC_STORE_RSS="RSSSource";    public static final char SEPARATOR_CHAR='-';    byte[] data=new byte[MAX_RSS_SOURCE+2+MAX_RSS_LINK];    private AddForm fmAddRSSSource;    private EditForm fmEditRSSSource;    public TitleList lsShowHeadLines=null;    private ArticleShowUI fmArticle;    public InfoForm fmInfo;    public RSSReader()    {        display=Display.getDisplay(this);        cmdExit=new Command("退出程序",Command.EXIT,1);        cmdAdd=new Command("添加RSS Feed源",Command.SCREEN,2);        cmdEdit=new Command("编辑RSS Feed源",Command.SCREEN,2);        cmdDelete=new Command("删除RSS Feed源",Command.SCREEN,2);        vecRSSSource=new Vector();        rsRSSSource=openRecordStore(REC_STORE_RSS);        readRSSList();        closeRecordStore(rsRSSSource);        lsRSSSource=new List("RSS Feed列表",List.IMPLICIT);        buildRSSList();        lsRSSSource.addCommand(cmdExit);        lsRSSSource.addCommand(cmdAdd);        lsRSSSource.addCommand(cmdEdit);        lsRSSSource.addCommand(cmdDelete);        lsRSSSource.setCommandListener(this);                fmAddRSSSource=new AddForm(this);        fmEditRSSSource=new EditForm(this);        fmArticle=new ArticleShowUI(this);        fmInfo=new InfoForm(this);    }    public void startApp()     {        showSourceForm();    }    public void pauseApp()     {    }    public void destroyApp(boolean unconditional)     {        deleteRecordStore(REC_STORE_RSS);        rsRSSSource=openRecordStore(REC_STORE_RSS);        writeRSSList();        closeRecordStore(rsRSSSource);    }    public void commandAction(Command command,Displayable displayable)    {        if(displayable==lsRSSSource&&command==List.SELECT_COMMAND)        {            if(lsShowHeadLines==null)            {                lsShowHeadLines=new TitleList(this);            }            RSSSourceEntry item=(RSSSourceEntry)vecRSSSource.elementAt(lsRSSSource.getSelectedIndex());            lsShowHeadLines.getTitles(item.getLink());            lsShowHeadLines.setTitle(item.getDescription());            showHeadlines();        }else if(command==cmdAdd)        {            showAddForm();        }else if(command==cmdEdit)        {            if(lsRSSSource.size()>0)            {                int i=lsRSSSource.getSelectedIndex();                RSSSourceEntry tmp=(RSSSourceEntry)vecRSSSource.elementAt(i);                String desc=tmp.getDescription();                String link=tmp.getLink();                fmEditRSSSource.setDescription(desc);                fmEditRSSSource.setLink(link);                fmEditRSSSource.setIndex(i);                showEditForm();            }        }else if(command==cmdDelete)        {            if(lsRSSSource.size()>1)            {                int i=lsRSSSource.getSelectedIndex();                lsRSSSource.delete(i);                vecRSSSource.removeElementAt(i);            }else            {                fmInfo.setMessage("不能删除最后一个...");            }        }else if(command==cmdExit)        {            destroyApp(false);            notifyDestroyed();        }    }    public void add2Vector(String description,String link)    {        RSSSourceEntry item=new RSSSourceEntry(description,link.toLowerCase());        vecRSSSource.insertElementAt(item, vecRSSSource.size());    }    public void showAddForm()    {        display.setCurrent(fmAddRSSSource);    }    public void showEditForm()    {        display.setCurrent(fmEditRSSSource);    }    public void showSourceForm()    {        display.setCurrent(this.lsRSSSource);    }    public void showInfoForm()    {        display.setCurrent(fmInfo);    }    public void showHeadlines()    {        display.setCurrent(lsShowHeadLines);    }    public void showArticleForm()    {        display.setCurrent(fmArticle);    }    public void setCurrent(Displayable displayable)    {        display.setCurrent(displayable);    }    private void deleteRecordStore(String name)    {        try        {            RecordStore.deleteRecordStore(name);        }catch(Exception e)        {            debug(e.toString());        }    }    private void closeRecordStore(RecordStore rs)    {        try        {            rs.closeRecordStore();        }catch(Exception e)        {            debug(e.getMessage());        }    }    private RecordStore openRecordStore(String name)    {        try        {            return RecordStore.openRecordStore(name, true);        }catch(Exception e)        {            debug(e.toString());            return null;        }    }    public void writeRSSList()    {        try        {            ByteArrayOutputStream baos=new ByteArrayOutputStream();            DataOutputStream dos=new DataOutputStream(baos);            byte[] rsData;            RSSSourceEntry item=null;            String desc,link;            for(int i=0;i<vecRSSSource.size();i++)            {                item=(RSSSourceEntry)vecRSSSource.elementAt(i);                String tmp=item.getDescription()+SEPARATOR_CHAR+item.getLink();                dos.writeUTF(tmp);                dos.flush();                rsData=baos.toByteArray();                rsRSSSource.addRecord(rsData, 0, data.length);                baos.reset();            }            baos.close();            dos.close();        }catch(Exception e)        {            debug(e.getMessage());        }    }    private void readRSSList()    {        try        {            if(rsRSSSource.getNumRecords()<1)            {                int i=DEFAULT_RSS_DESCRIPTIONS.length;                for(int j=0;j<i;j++)                {                    add2Vector(DEFAULT_RSS_DESCRIPTIONS[j],DEFAULT_RSS_LINKS[j]);                }                return;            }            ByteArrayInputStream bais=new ByteArrayInputStream(data);            DataInputStream dis=new DataInputStream(bais);            RSSSourceEntry item=null;            String tmp_rms,tmp_parser;            int offset;            for(int i=1;i<rsRSSSource.getNumRecords();i++)            {                rsRSSSource.getRecord(i, data, 0);                tmp_rms=dis.readUTF();                offset=tmp_rms.indexOf(SEPARATOR_CHAR);                item=new RSSSourceEntry(tmp_rms.substring(0, offset),tmp_rms.substring(offset+1, tmp_rms.length()));                vecRSSSource.addElement(item);                bais.reset();            }            bais.close();            dis.close();        }catch(Exception e)        {            debug(e.toString());        }    }    private void buildRSSList()    {        RSSSourceEntry item=null;        for(int i=0;i<vecRSSSource.size();i++)        {            item=(RSSSourceEntry)vecRSSSource.elementAt(i);            lsRSSSource.append(item.getDescription(), null);        }    }    public void debug(String message)    {        fmInfo.setMessage(message);    }    public void setArticleTitle(String title)    {        fmArticle.setArticleTitle(title);    }    public void setArticleContent(String content)    {        fmArticle.setArticleContent(content);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -