不知道为什么一直有一个想法把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.youkuaiyun.com/source/561370
本文介绍了一种使用Flex DataGrid展示CUBlog排行榜数据的方法,通过正则表达式从网页提取所需信息,并利用Flex与Java后端交互完成数据展示。
931

被折叠的 条评论
为什么被折叠?



