oschina安卓源代码解析(四)构造新闻实体类

接上面的,那怎么去服务器端获取那个数据内容后再在客户端显示出来呢。

这里我们构造一个新闻实体类,映射数据库的新闻表。

public class News extends Entity{
        
        private static final long serialVersionUID = -8855459365192170589L;
        public final static String NODE_ID = "id";
        public final static String NODE_TITLE = "title";
        public final static String NODE_URL = "url";
        public final static String NODE_BODY = "body";
        public final static String NODE_AUTHORID = "authorid";
        public final static String NODE_AUTHOR = "author";
        public final static String NODE_PUBDATE = "pubDate";
        public final static String NODE_COMMENTCOUNT = "commentCount";
        public final static String NODE_FAVORITE = "favorite";
        public final static String NODE_START = "news";
        
        public final static String NODE_SOFTWARELINK = "softwarelink";
        public final static String NODE_SOFTWARENAME = "softwarename";
        
        public final static String NODE_NEWSTYPE = "newstype";
        public final static String NODE_TYPE = "type";
        public final static String NODE_ATTACHMENT = "attachment";
        public final static String NODE_AUTHORUID2 = "authoruid2";
        
        public final static int NEWSTYPE_NEWS = 0x00;//0 新闻
        public final static int NEWSTYPE_SOFTWARE = 0x01;//1 软件
        public final static int NEWSTYPE_POST = 0x02;//2 帖子
        public final static int NEWSTYPE_BLOG = 0x03;//3 博客

        private String title;
        private String url;
        private String body;
        private String author;
        private int authorId;
        private int commentCount;
        private String pubDate;
        private String softwareLink;
        private String softwareName;
        private int favorite;
        private NewsType newType;
        private List<Relative> relatives;

        public News(){
                this.newType = new NewsType();
                this.relatives = new ArrayList<Relative>();
        }        
        
        public class NewsType implements Serializable{
                private static final long serialVersionUID = 3466703975731604908L;
                public int type;
                public String attachment;
                public int authoruid2;
        }
        
        public static class Relative implements Serializable{
                private static final long serialVersionUID = 7585117599244383997L;
                public String title;
                public String url;
        }
        
        public List<Relative> getRelatives() {
                return relatives;
        }
        public void setRelatives(List<Relative> relatives) {
                this.relatives = relatives;
        }
        public NewsType getNewType() {
                return newType;
        }
        public void setNewType(NewsType newType) {
                this.newType = newType;
        }        
        public int getFavorite() {
                return favorite;
        }
        public void setFavorite(int favorite) {
                this.favorite = favorite;
        }
        public String getSoftwareLink() {
                return softwareLink;
        }
        public void setSoftwareLink(String softwareLink) {
                this.softwareLink = softwareLink;
        }
        public String getSoftwareName() {
                return softwareName;
        }
        public void setSoftwareName(String softwareName) {
                this.softwareName = softwareName;
        }
        public String getPubDate() {
                return this.pubDate;
        }
        public void setPubDate(String pubDate) {
                this.pubDate = pubDate;
        }
        public String getTitle() {
                return title;
        }
        public void setTitle(String title) {
                this.title = title;
        }
        public String getUrl() {
                return url;
        }
        public void setUrl(String url) {
                this.url = url;
        }
        public String getBody() {
                return body;
        }
        public void setBody(String body) {
                this.body = body;
        }
        public String getAuthor() {
                return author;
        }
        public void setAuthor(String author) {
                this.author = author;
        }
        public int getAuthorId() {
                return authorId;
        }
        public void setAuthorId(int authorId) {
                this.authorId = authorId;
        }
        public int getCommentCount() {
                return commentCount;
        }
        public void setCommentCount(int commentCount) {
                this.commentCount = commentCount;
        }
        
        public static News parse(InputStream inputStream) throws IOException, AppException {
                News news = null;
                Relative relative = null;
        //获得XmlPullParser解析器
        XmlPullParser xmlParser = Xml.newPullParser();
        try {               
            xmlParser.setInput(inputStream, UTF8);
            //获得解析到的事件类别,这里有开始文档,结束文档,开始标签,结束标签,文本等等事件。
            int evtType=xmlParser.getEventType();
                        //一直循环,直到文档结束   
                        while(evtType!=XmlPullParser.END_DOCUMENT){
                            String tag = xmlParser.getName();
                            switch(evtType){
                                    case XmlPullParser.START_TAG:
                                            if(tag.equalsIgnoreCase(NODE_START))
                                            {
                                                    news = new News();
                                            }
                                            else if(news != null)
                                            {        
                                            if(tag.equalsIgnoreCase(NODE_ID))
                                            {                              
                                                    news.id = StringUtils.toInt(xmlParser.nextText(),0);
                                            }
                                            else if(tag.equalsIgnoreCase(NODE_TITLE))
                                            {                                            
                                                    news.setTitle(xmlParser.nextText());
                                            }
                                            else if(tag.equalsIgnoreCase(NODE_URL))
                                            {                                            
                                                    news.setUrl(xmlParser.nextText());
                                            }
                                            else if(tag.equalsIgnoreCase(NODE_BODY))
                                            {                                            
                                                    news.setBody(xmlParser.nextText());
                                            }
                                            else if(tag.equalsIgnoreCase(NODE_AUTHOR))
                                            {                                            
                                                    news.setAuthor(xmlParser.nextText());                                    
                                            }
                                            else if(tag.equalsIgnoreCase(NODE_AUTHORID))
                                            {                                            
                                                    news.setAuthorId(StringUtils.toInt(xmlParser.nextText(),0));                                    
                                            }
                                            else if(tag.equalsIgnoreCase(NODE_COMMENTCOUNT))
                                            {                                            
                                                    news.setCommentCount(StringUtils.toInt(xmlParser.nextText(),0));                                            
                                            }
                                            else if(tag.equalsIgnoreCase(NODE_PUBDATE))
                                            {                                            
                                                    news.setPubDate(xmlParser.nextText());              
                                            }        
                                            else if(tag.equalsIgnoreCase(NODE_SOFTWARELINK))
                                            {                                            
                                                    news.setSoftwareLink(xmlParser.nextText());                                            
                                            }        
                                            else if(tag.equalsIgnoreCase(NODE_SOFTWARENAME))
                                            {                                            
                                                    news.setSoftwareName(xmlParser.nextText());                                            
                                            }        
                                            else if(tag.equalsIgnoreCase(NODE_FAVORITE))
                                            {                                            
                                                    news.setFavorite(StringUtils.toInt(xmlParser.nextText(),0));                                    
                                            }
                                            else if(tag.equalsIgnoreCase(NODE_TYPE))
                                            {        
                                                    news.getNewType().type = StringUtils.toInt(xmlParser.nextText(),0);
                                            }
                                            else if(tag.equalsIgnoreCase(NODE_ATTACHMENT))
                                            {                                            
                                                    news.getNewType().attachment = xmlParser.nextText();         
                                            }
                                            else if(tag.equalsIgnoreCase(NODE_AUTHORUID2))
                                            {                                            
                                                    news.getNewType().authoruid2 = StringUtils.toInt(xmlParser.nextText(),0);
                                            }
                                            else if(tag.equalsIgnoreCase("relative"))
                                            {                                            
                                                    relative = new Relative();
                                            }
                                            else if(relative != null)
                                            {                                            
                                                    if(tag.equalsIgnoreCase("rtitle"))
                                                    {
                                                            relative.title = xmlParser.nextText();         
                                                    }
                                                    else if(tag.equalsIgnoreCase("rurl"))
                                                    {
                                                            relative.url = xmlParser.nextText();         
                                                    }
                                            }
                                            //通知信息
                                            else if(tag.equalsIgnoreCase("notice"))
                                                    {
                                                    news.setNotice(new Notice());
                                                    }
                                            else if(news.getNotice() != null)
                                                    {
                                                            if(tag.equalsIgnoreCase("atmeCount"))
                                                    {                              
                                                                    news.getNotice().setAtmeCount(StringUtils.toInt(xmlParser.nextText(),0));
                                                    }
                                                    else if(tag.equalsIgnoreCase("msgCount"))
                                                    {                                            
                                                            news.getNotice().setMsgCount(StringUtils.toInt(xmlParser.nextText(),0));
                                                    }
                                                    else if(tag.equalsIgnoreCase("reviewCount"))
                                                    {                                            
                                                            news.getNotice().setReviewCount(StringUtils.toInt(xmlParser.nextText(),0));
                                                    }
                                                    else if(tag.equalsIgnoreCase("newFansCount"))
                                                    {                                            
                                                            news.getNotice().setNewFansCount(StringUtils.toInt(xmlParser.nextText(),0));
                                                    }
                                                    }
                                            }
                                            break;
                                    case XmlPullParser.END_TAG:               
                                            //如果遇到标签结束,则把对象添加进集合中
                                               if (tag.equalsIgnoreCase("relative") && news!=null && relative!=null) {
                                                       news.getRelatives().add(relative);
                                                       relative = null;
                                               }
                                               break;
                            }
                            //如果xml没有结束,则导航到下一个节点
                            evtType=xmlParser.next();
                        }               
        } catch (XmlPullParserException e) {
                        throw AppException.xml(e);
        } finally {
                inputStream.close();        
        }      
        return news;      
        }
}

看上面代码很多,其实只要看几个字段和后面的那个解析输入流的静态方法就好了。

private String title;
        private String url;
        private String body;
        private String author;
        private int authorId;
        private int commentCount;
        private String pubDate;
        private String softwareLink;
        private String softwareName;
        private int favorite;
        private NewsType newType;
        private List<Relative> relatives;

上面这些就是数据库表里面的字段,反正看英文名字大家都能看懂是什么意思。
最后面是一个静态方法,用于解析一个Inputstream,把输入流转换成实体类的集合
基本原理:
通过上一节的内容,不是有一个地址吗
http://www.oschina.net/action/api/news_list?catalog=1&pageIndex=0&pageSize=20
我们打开这个链接时候显示的是跟打开一个xml文件是一样的,我们要做的是,用一个封装好的get方式去访问这个地址。
然后获取这个地址上面的内容。Java真的很强大,可以直接把网页内容转换成流的形式,把网页上面的内容,转换成输入流。
那这个输入流就可以在这个实体类里面使用,解析这个输入流,把他转换出来变成一个List<News>集合数据。是不是很容易,
作为一个数据集去放到控件里面了。ListView。
今天就说到这里吧。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值