`
yexin218
  • 浏览: 959042 次
  • 性别: Icon_minigender_1
  • 来自: 珠海
社区版块
存档分类
最新评论

Flex-CUBlog排行榜解析

    博客分类:
  • Flex
阅读更多

不知道为什么一直有一个想法把CUBlog排行榜的数据提取出来放在Flex的DataGrid中浏览。上网看过了关于html解析有很多开源的比如 html parser等。但好像都要写蛮多的代码才能实现。一个比较简单快速的方法就是使用正则表达式了。刚学习,顺便练习下。效果图:


Flex和后台Java的沟通方式是采用remoteObject。直接贴代码把,很简单不用解释。
前台Flex处理:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Style>

    </mx:Style>
    <mx:Script>
        <![CDATA[
        import mx.rpc.events.ResultEvent;
        import mx.collections.ArrayCollection;
        import com.adobe.serialization.json.JSON;

        [Bindable]
        private var dataArray:ArrayCollection;

        private function initDataGrid():void{
            dataArray = new ArrayCollection();
        }

        private function getBolgListFun():void{
            var blogIndex:int;
            blogIndex = showId.selectedIndex;
            getBlog.getList(blogIndex);
            labTxt.text="请稍候,正在读取博客列表...你选择查看"+showId.selectedItem.toString()+"的排行榜!";
        }
        private function getBlogListResult(event:ResultEvent):void{
            var rawArray:Array;
            var arraySize:int;
            var rawData:String = event.result as String;
            rawArray = JSON.decode(rawData) as Array;
            dataArray = new ArrayCollection(rawArray);
            arraySize = dataArray.length;
            labTxt.text="读取成功...";
        }
        ]]>
    </mx:Script>
    <mx:RemoteObject id="getBlog" destination="getBlogList" showBusyCursor="true" result="getBlogListResult(event)"/>
    <mx:Panel x="10" y="40" width="681" height="430" layout="absolute" title="CU博客排行版">
        <mx:DataGrid id="blogGrid" x="0" y="0" width="661" height="284" dataProvider="{dataArray}" creationComplete="{initDataGrid()}" verticalScrollPolicy="on" fontFamily="Times New Roman" fontSize="12" color="#0142FC">
            <mx:columns>
                <mx:DataGridColumn headerText="名次" dataField="no"/>
                <mx:DataGridColumn headerText="博客名称" dataField="blog" width="220"/>
                <mx:DataGridColumn headerText="用户名" dataField="user" width="80"/>
                <mx:DataGridColumn headerText="技术文章" dataField="technique"/>
                <mx:DataGridColumn headerText="总文章" dataField="total"/>
                <mx:DataGridColumn headerText="访问量" dataField="visit"/>
                <mx:DataGridColumn headerText="评论量" dataField="common"/>
                <mx:DataGridColumn headerText="推荐数" dataField="recommand"/>
            </mx:columns>
        </mx:DataGrid>
        <mx:Button id="getBtn" x="10" y="323" label="读取更新" width="474" height="35" click="getBolgListFun()" fontFamily="Times New Roman" fontSize="12" color="#02FEF5"/>
        <mx:Label id="labTxt" x="0" y="292" width="626" height="23" fontFamily="Times New Roman" fontSize="12" color="#FE0315"/>
        <mx:ComboBox x="492" y="323" width="134" height="35" fontSize="12" fontFamily="Times New Roman" color="#A103FC" selectedIndex="3" id="showId">
         <mx:dataProvider>
             <mx:Array>
                 <mx:String>技术文章</mx:String>
                 <mx:String>推荐总数</mx:String>
                 <mx:String>总文章数</mx:String>
                 <mx:String>总访问量</mx:String>
                 <mx:String>评论总数</mx:String>
             </mx:Array>
         </mx:dataProvider>
        </mx:ComboBox>
    </mx:Panel>

</mx:Application>

 后台Java处理:

package cublog;
/*
 * @Author: yexin218
 * @Email:feixianyexin@qq.com
 */
import java.net.*;
import java.io.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class PaiHangBang {
    public static void main(String[] args){
        PaiHangBang p = new PaiHangBang();
        System.out.println(p.getList(3));
    }
    public String getList(int selectIndex){
        String blogList="";
        int showBlogNo=25;//the no of blog to show

        StringBuffer document = new StringBuffer();
        try{
        URL url = new URL("http://blog.chinaunix.net/top/?type="+selectIndex);
        URLConnection conn = url.openConnection();
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line = null;
        String result=null;
        while ((line = reader.readLine()) != null)
        document.append(line+"|");
        result = document.toString().replaceAll("<[^>]*>","").replaceAll("\\s+","");
        result = result.substring(result.indexOf("推荐数")+6, result.indexOf("关于我们"));

        String array[] = result.split("\\|");
        String str="";
        for(int i=0;i<array.length;i++){
            if(!array[i].equals("")){
                str+=array[i]+"|";
            }

        }
         JSONArray blogListArray = new JSONArray();
         JSONObject blogObj = new JSONObject();
        String newBlogArray[] = str.split("\\|");//here is all the item independently

        for(int i=0;i<showBlogNo;i++){//The head 20 items

            blogObj.put("no", newBlogArray[8*i]);
            blogObj.put("blog", newBlogArray[8*i+1]);
            blogObj.put("user", newBlogArray[8*i+2]);
            blogObj.put("technique", newBlogArray[8*i+3]);
            blogObj.put("total", newBlogArray[8*i+4]);
            blogObj.put("visit", newBlogArray[8*i+5]);
            blogObj.put("common", newBlogArray[8*i+6]);
            blogObj.put("recommand", newBlogArray[8*i+7]);
            blogListArray.add(blogObj);
        }

      blogList = blogListArray.toString();
     reader.close();
    }catch(MalformedURLException e){
        e.printStackTrace();
    }catch(IOException ex){
        ex.printStackTrace();
    }
    System.out.println(blogList);
    return blogList;
    }

}

 前后台之间需要的remote-config.xml配置。增加以下语句:

<destination id="getBlogList">
     <properties>
         <source>cublog.PaiHangBang</source>
       </properties>
 </destination><destination id="getBlogList">
     <properties>
         <source>cublog.PaiHangBang</source>
       </properties>
 </destination>

  Eclipse工程文件下载:http://download.csdn.net/source/561370

2
2
分享到:
评论

相关推荐

    NiosII开发文档大全

    NIOS2软件架构解析读后感高手篇.pdf NiosII_SOPCBuilder_5_1_Labs_for_DE2.pdf NiosII之IDE属性配置(Sep29更新,静态密码:NiosII.cublog.cn).pdf NiosII之处理器操作模式及寄存器(Sep29更新,静态密码:NiosII....

    shell0.10.0解析版

    解压密码:skyily.cublog.cn 第二版本 0.10.1 第0步骤: 1. 要求每个命令完成后,重新打印出提示符 2. 测试:ls cat 等命令 第1步骤:参数 1. 提示符中出现当前路径信息。 2. 测试命令“ls”,“ls&lt;空格&gt;”,“...

    CopyPath v1.0, Copy paths of the selected files, (新增右键支持复制多文件路径)

    http://sxg.cublog.cn/ http://blog.chinaunix.net/u/8754/showart_1961481.html Important Notice: Althouth there are no malicious codes included, I do not issue any guarantee of any kind, use it at your...

    SHELL经典笔试题及答案 小实例

    SHELL经典笔试题及答案 小实例,欢迎大家一起探讨 File information 2009-8-21 磁针石:xurongzhong#gmail.com 博客:oychw.cublog.cn

    WereWolf

    WereWolf

    ubuntu硬盘非wubi安装自结

    出自博客:quietheart.cublog.cn 这篇文章讲述如何从硬盘上安装ubuntu,参考网上资源,并且亲身实践ubuntu9.10安装成功。总结并分享如下: 最简单介绍: =================== 1.准备文件: 1)grub4dos相关文件(grldr...

    VPCS 0.21a

    站点:http://wiki.freecode.com.cn 或 http://mirnshi.cublog.cn 历史版本: 0.21a 修订IPv6的RS 0.21 修订了许多问题 0.20b 支持与外部tcpserver通信 0.20a 进一步增强IPv6,支持LinkLocal,无状态自动配置,...

    ABAP技术开发总结

    从 kevinsky.cublog.cn 整理的,感觉写的挺好的,传上来大家分享一下,也赚点分,呵呵

    CS8900_linux-2.6.24.4.rar_2410 cs8900_S3C2410 CS8900 _cs8900_cs8

    linux2.6.24 S3C2410下的网卡CS8900驱动 具体移植方法可以参考我的blog http://www.cublog.cn/u2/63560/showart_514147.html

    ELF文件格式学习

    ELF文件格式学习 From: http://danielwood.cublog.cn

    如何学好nios

    学习nios的好资料 打开密码:NiosII.cublog.cn

    packet tracer 示例

    转载于:|狼人◇_传说 http://werewolf.cublog.cn/

    Android_GPS架构分析

    转载时请注明出处和作者 文章出处:http://danielwood.cublog.cn 作者:Daniel Wood

    URLTester

    最新版本为2.3.1,有关URLTester的最新信息发布在http://aquester.cublog.cn上。 网址:http://blog.chinaunix.net/u2/64804/showart_1132881.html&lt;br&gt;下载:...

    针对mini2440的Android内核镜像

    针对老版的supervivi进行了mach_type的修改(与FriendlyArm论坛的内核镜像不同),我的开发板是两年前的,用户...具体的文件系统可以到FriendlyArm的官方论坛下载:http://www.cublog.cn/u3/97285/showart_1967792.html

    dlmalloc说明及代码

    lenky0401个人博客将陆续推出对dlmalloc的解析(针对Doug Lea Malloc的最新版Version 2.8.3,未做说明的情况下以32位平台,8字节对齐作为假定平台环境设置考虑),由于个人水平有限,因此也不能完全保证对dlmalloc的...

    vpcs0.21a,gns的好搭档

    站点:http://wiki.freecode.com.cn 或 http://mirnshi.cublog.cn 历史版本: 0.21a 修订IPv6的RS 0.21 修订了许多问题 0.20b 支持与外部tcpserver通信 0.20a 进一步增强IPv6,支持LinkLocal,无状态自动配置,...

    NiosII之动态栈溢出检测功能

    NiosII之动态栈溢出检测功能,中文版的资料 蔡伟刚编写。 密码:NiosII.cublog.cn(区分大小写)

    给Apache虚拟主机增加端口的方法

    找到你的apache安装目录,找到httpd.conf文件,  搜索#listen这一句,在下面增加  代码如下:listen 800 ...# serveradmin webmaster@dummy-host2.phps教程hao.cublog.cn # documentroot /www/docs/dummy-host2.ph

Global site tag (gtag.js) - Google Analytics