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

📄 rssreader.java

📁 这是我们学校教的j2me程序开发实例从入门到精通自带关盘的源代码
💻 JAVA
字号:
/*
 * RSSReader.java
 *
 * Created on 2006年12月20日, 上午2:01
 */

package cn.edu.uestc.pandarss;

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

/**
 *
 * @author  pandaxiaoxi(blog.csdn.net/pandaxiaoxi)
 * @version
 */

public class RSSReader extends MIDlet implements CommandListener {
    protected Display display; // Display对象的引用
    
    protected List lsRSSSources; // RSS源的主列表
    private Command cmExit; // Exit命令
    private Command cmAdd; // 添加新RSS源命令
    private Command cmEdit; // 编辑新RSS源命令
    private Command cmDelete; // 删除RSS源命令
    protected static final int MAX_RSS_SOURCE = 15;
    protected static final int MAX_RSS_LINK = 75;
    protected static final String[] DEFAULT_RSS_DESCRIPTIONS =
    {
        "米亚罗", "aa",
        "苹果老鼠哆来咪",};
    protected static final String[] DEFAULT_RSS_LINKS =
    {
        "http://blog.csdn.net/pandaxiaoxi/rss.aspx",
        "http://www.quotationspage.com/data/qotd.rss",
        "http://blog.sina.com.cn/myblog/index_rss.php?uid=1222809845",
    };
    //RSS源的主列表,包含'RSS源'对象
    protected Vector vecRSSSources;
    //记录存储的引用
    private RecordStore rsRSSSources;
    // 记录存储的名字
    private static final String REC_STORE_RSS = "RSSSources";
    // 在RMS中RSS源的名称和链接用‘-’分隔
    protected static final char SEPARATOR_CHAR = '-';
    // 在RMS中的每一个Entity,包含RSS源的名称,分隔符和链接
    byte[] rsData = new byte[MAX_RSS_SOURCE + 2 + MAX_RSS_LINK];
    // 添加RSS源的Form
    private AddForm fmAddRSSSource; 
    // 编辑RSS源的Form
    private EditForm fmEditRSSSource; 
    // 文章标题的列表
    protected TitleList lsShowHeadlines = null; 
    // 显示选中的文章的内容摘要
    protected ArticleDisplayUI fmArticle; 
    // 显示状态信息的Form
    protected InfoForm fmInfo; 
    public RSSReader() {
        display = Display.getDisplay(this);
        
        cmExit = new Command("Exit", Command.EXIT, 1);
        cmAdd = new Command("Add", Command.SCREEN, 2);
        cmEdit = new Command("Edit", Command.SCREEN, 3);
        cmDelete = new Command("Delete", Command.SCREEN, 4);
        // 创建主RSS列表和命令
        vecRSSSources = new Vector();
        // 打开或创建记录存储
        rsRSSSources = openRecStore(REC_STORE_RSS);
        // 从RMS中读取RSS源
        readRSSList();
        // 关闭记录存储
        closeRecStore(rsRSSSources);
        // 创建RSS源列表控件
        lsRSSSources = new List("RSS Feed列表", List.IMPLICIT);
        // 添加Entries到RSS源列表控件
        buildRSSList();
        
        lsRSSSources.addCommand(cmExit);
        lsRSSSources.addCommand(cmAdd);
        lsRSSSources.addCommand(cmEdit);
        lsRSSSources.addCommand(cmDelete);
        lsRSSSources.setCommandListener(this);
        
        fmAddRSSSource = new AddForm(this);
        
        fmEditRSSSource = new EditForm(this);
        
        fmArticle = new ArticleDisplayUI(this);
        
        fmInfo = new InfoForm(this);
        
    }

    public void startApp() {
        display.setCurrent(lsRSSSources);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
        //删除原有的RMS
        deleteRecStore(REC_STORE_RSS);
        // 打开RMS
        rsRSSSources = openRecStore(REC_STORE_RSS);
        // 把向量写回RMS
        writeRSSList();
        closeRecStore(rsRSSSources);
    }
/*--------------------------------------------------
 * 事件处理
 *-------------------------------------------------*/
    public void commandAction(Command c, Displayable s) {
        // 选择某个RSS Feed源
        if (s == lsRSSSources && c == List.SELECT_COMMAND) {
            
            if (lsShowHeadlines == null) {
                lsShowHeadlines = new TitleList(this);
// Get a reference to the current (selected) entry in the vector.
// (the vector contains NewsSourceObjects that hold descriptions
// and http links to the actual news source)
            }
            RSSSourceEntry item = (RSSSourceEntry) vecRSSSources.elementAt(
                 lsRSSSources.getSelectedIndex());
            // 使用RSS Feed源的链接,
            // 创建属于该RSS Feed源的所有文章标题列表
            lsShowHeadlines.getTitles(item.getLink());
           
            lsShowHeadlines.setTitle(item.getDescription());
            //在设备屏幕上显示该RSS Feed源的所有文章标题列表界面
            display.setCurrent(lsShowHeadlines);
        } else if (c == cmAdd) {
            // 显示添加RSS Feed源界面
            display.setCurrent(fmAddRSSSource);
        } else if (c == cmEdit) {
// 编辑当前选中的RSS Feed源
// 显示编辑RSS Feed源界面
// 界面上显示该RSS Feed源原来的名称和链接
            if (lsRSSSources.size() > 0) {
                int i = lsRSSSources.getSelectedIndex();
                RSSSourceEntry tmp = (RSSSourceEntry) vecRSSSources.elementAt(i);
                String description = tmp.getDescription();
                String link = tmp.getLink();
                
                fmEditRSSSource.setDescription(description);
                fmEditRSSSource.setLink(link);
                fmEditRSSSource.setIndex(lsRSSSources.getSelectedIndex());
                
                display.setCurrent(fmEditRSSSource);
            }
        } else if (c == cmDelete) {
            //仅在有大于1个RSS Feed源的情况,才可以删除RSS Feed源
            if (lsRSSSources.size() > 1) {
                int i = lsRSSSources.getSelectedIndex();
                lsRSSSources.delete(i);
                vecRSSSources.removeElementAt(i);
            } else {
                fmInfo.setMessage("不能删除最后一个..."); 
            }
        } else if (c == cmExit) {
            destroyApp(false);
            notifyDestroyed();
        }
    }
/*--------------------------------------------------
 * 打开记录存储
 *-------------------------------------------------*/
    private RecordStore openRecStore(String name) {
        try {
            //打开记录存储,如果没有就创建新的
            return RecordStore.openRecordStore(name, true);
        } catch (Exception e) {
            db(e.toString());
            return null;
        }
    }
/*--------------------------------------------------
 * 关闭记录存储
 *-------------------------------------------------*/
    private void closeRecStore(RecordStore rs) {
        try {
            rs.closeRecordStore();
        } catch (Exception e) {
            db(e.toString());
        }
    }
/*--------------------------------------------------
 * 删除记录存储
 *-------------------------------------------------*/
    private void deleteRecStore(String name) {
        try {
            RecordStore.deleteRecordStore(name);
        } catch (Exception e) {
            db(e.toString());
        }
    }
/*------------------------------------------------------
 * 加载RMS中存储的RSS Feed源到RSSSourceEntry类型的向量vecRSSSources中,
 * 该向量在本类中定义
 *-----------------------------------------------------*/
    private void readRSSList() {
        
                try {
            // 如果RMS中没有存储RSS Feed源,就把硬编码在本类中的RSS Feed源添加进
            // 向量vecRSSSources中
            if (rsRSSSources.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 strmBytes = new ByteArrayInputStream(rsData);
            DataInputStream strmDataType = new DataInputStream(strmBytes);
            RSSSourceEntry item;
            String tmp_rms, tmp_parse;
            int offset;
            for (int i = 1; i <= rsRSSSources.getNumRecords(); i++) {
                // 从RMS读取一条记录存到rsData中
                rsRSSSources.getRecord(i, rsData, 0);
                tmp_rms = strmDataType.readUTF();
                // 基于名称和链接之间SEPARATOR_CHAR
                // 解析出该字符串中包含的RSS Feed源的名称和链接
                offset = tmp_rms.indexOf(SEPARATOR_CHAR); // 分隔符的偏移
                // 根据RSS Feed源的名称和链接构造RSSSourceEntry对象
                item = new RSSSourceEntry(tmp_rms.substring(0, offset),
                        tmp_rms.substring(offset + 1, tmp_rms.length()));
                // 添加到向量vecRSSSources中
                vecRSSSources.addElement(item);
                // 重设strmBytes,以便可以再次从该流的开头处读取数据
                strmBytes.reset();
            }
            strmBytes.close();
            strmDataType.close();
        } catch (Exception e) {
            db(e.toString());
        }
    }
/*------------------------------------------------------
 * 添加RSS Feed源到向量vecRSSSources中
 *-----------------------------------------------------*/
    protected void add2Vector(String description, String link)
    
    {
        RSSSourceEntry item = new RSSSourceEntry(description,
                link.toLowerCase());
        // 插到末尾
        vecRSSSources.insertElementAt(item, vecRSSSources.size());
    }
/*------------------------------------------------------
 * 使用vecRSSSources向量构造RSS Feed源列表控件
 *-----------------------------------------------------*/
    private void buildRSSList() {
        RSSSourceEntry item;
        for (int i = 0; i < vecRSSSources.size(); i++) {
            item = (RSSSourceEntry) vecRSSSources.elementAt(i);
            lsRSSSources.append(item.getDescription(), null);
        }
    }
/*------------------------------------------------------
 * 把向量vecRSSSources中包含的所有RSS Feed源写回RMS
 *-----------------------------------------------------*/
    private void writeRSSList() {
        try {
            // 把数据写入字节数组输出流
            ByteArrayOutputStream strmBytes = new ByteArrayOutputStream();
            // 写Java类型的数据到前面的字节数组输出流
            DataOutputStream strmDataType = new DataOutputStream(strmBytes);
            byte[] record;
            RSSSourceEntry item;
            String description;
            String link;
            for (int i = 0; i < vecRSSSources.size(); i++) {
                item = (RSSSourceEntry) vecRSSSources.elementAt(i);
                // 构造包含RSS Feed源名称、分隔符和链接的字符串
                String tmp = item.getDescription() + SEPARATOR_CHAR + item.getLink();
                strmDataType.writeUTF(tmp);
                // 刷新缓存
                strmDataType.flush();
                record = strmBytes.toByteArray();
                rsRSSSources.addRecord(record, 0, record.length);
                // 重设strmBytes,以便可以再次从该流的开头处写入数据
                strmBytes.reset();
            }
            strmBytes.close();
            strmDataType.close();
        } catch (Exception e) {
            db(e.toString());
        }
    }
/*--------------------------------------------------
 * 调用InfoForm对象显示状态调试信息
 *-------------------------------------------------*/
    private void db(String str) {
        fmInfo.setMessage("Msg: " + str); 
    }
}

⌨️ 快捷键说明

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