软件主界面采用Fragment+ViewPager组成。在点开后将会自动对学校新闻页面使用URl类来抓取,然后对网页中的信息提取,使用WebView来loadData在主界面上面显示,
为了使用方便,软件会将最近一次抓取结果,写入文件中,在没有网络时,可以查看最近的缓存新闻。
在技术上用到了,Handler类,用于子线程与Activity通信,Intent基本使用,网络操作基本类URL,Fragmen+ViewPager支持滑动布局,调用系统分享功能等等。
项目结构:
部分核心代码摘要:
// 列表解析
private boolean url_data_download(final String url) {
if (!isRunning) {
new Thread() {
@Override
public void run() {
// TODO 自动生成的方法存根
super.run();
isRunning = true;
String url_str;
String url_data = null;
try {
URL ncwuURL = new URL(url);
// 加"gbk",解决一bug
InputStreamReader in = new InputStreamReader(
ncwuURL.openStream(), "gbk");
BufferedReader reader = new BufferedReader(in);
while ((url_str = reader.readLine()) != null) {
url_data += url_str;
}
if (url_data != null) {
String url_datas[] = url_data
.split("<TD vAlign=top height=12 >");
String url_datass[] = url_datas[1]
.split("<TD vAlign=top height=10></TD>");
System.out.println(url_datass[0]);
datas = url_datass[0];
if (!isPaused) {
Message m = data_handler.obtainMessage();
Bundle resBund = new Bundle();
resBund.putString("result", "获取数据成功...");
m.setData(resBund);
m.what = 1;
data_handler.sendMessage(m);
}
} else {
Message m = data_handler.obtainMessage();
Bundle resBund = new Bundle();
resBund.putString("result", "获取数据失败...");
m.setData(resBund);
m.what = 0;
data_handler.sendMessage(m);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
return false;
}
// 详情页解析
private boolean Detail_data_download(final String url) {
notice_state.setText("正在获取最新数据。。。");
new Thread() {
@Override
public void run() {
// TODO 自动生成的方法存根
super.run();
String url_str;
String url_data = null;
try {
URL ncwuURL = new URL(url);
// 加"gbk",解决一bug
InputStreamReader in = new InputStreamReader(
ncwuURL.openStream(), "gbk");
BufferedReader reader = new BufferedReader(in);
while ((url_str = reader.readLine()) != null) {
url_data += url_str;
}
} catch (MalformedURLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
if (url_data != null) {
String url_datas[] = url_data
.split("BORDER-LEFT-STYLE: dotted; BORDER-RIGHT-STYLE: dotted; BORDER-TOP-STYLE: dotted\" color=\"#BCE5F5\"/></span>");
String url_datass[] = url_datas[1]
.split(" <td><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
// System.out.println(url_datass[0]);
urldatasDetailsString = url_datass[0];
Message m = data_handler.obtainMessage();
Bundle resBund = new Bundle();
resBund.putString("result", "获取数据成功...");
m.setData(resBund);
m.what = 3;
data_handler.sendMessage(m);
} else {
Message m = data_handler.obtainMessage();
Bundle resBund = new Bundle();
resBund.putString("result", "获取数据失败...");
m.setData(resBund);
m.what = 4;
data_handler.sendMessage(m);
}
}
}.start();
return false;
}
public class MyPageAdapter extends FragmentStatePagerAdapter {
private List<Fragment> listFragment = new ArrayList<Fragment>();
public MyPageAdapter(FragmentManager fm) {
super(fm);
}
public void setListFragment(List<Fragment> listFragment) {
this.listFragment = listFragment;
}
@Override
public Fragment getItem(int arg0) {
return listFragment.get(arg0);
}
@Override
public int getCount() {
return listFragment.size();
}
}