H:/1217/01_Java采集网页_.java
package
package cn.itcast.data;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.HasParentFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.nodes.TextNode;
import org.htmlparser.util.NodeList;
public class TestData {
/***
* 抓取数据的代码
*/
public static void main(String[] args) {
try {
// 1.创建html解析器
Parser p = new Parser();
// 2.设置需要解析的html URL路径
p.setURL("http://zhushou.360.cn/list/index/cid/1?page=1");
// 3.只获得li节点
// 3.1.创建属性过滤器,属性id ==iconlist
HasAttributeFilter haf = new HasAttributeFilter("id", "iconList");
// 3.2.创建父过滤器,父结点必须是 属性id ==iconlist的haf
HasParentFilter hpf = new HasParentFilter(haf);
// 3.3.创建且过滤器,即又要有特定的父结点,又必须标签名叫li
AndFilter af = new AndFilter(hpf, new TagNameFilter("li"));
// 3.4.通过最终的过滤器,过滤出想要的li结点列表
NodeList nodeList = p.parse(af);
// sb拼装成Json供Xcode使用
StringBuffer sb = new StringBuffer();
sb.append("@[");
for (int i = 0; i < nodeList.size(); i++) {
// 4.1.获得了对应li节点
TagNode liNode = (TagNode) nodeList.elementAt(i);
// 4.2.获得li里面的A标签里面的img图片(img节点)
NodeList liChildren = liNode.getChildren();
TagNode imgNode = (TagNode) liChildren.elementAt(0).getChildren().elementAt(0);
String url = imgNode.getAttribute("_src");
// 4.3.获得软件名称
TextNode nameNode = (TextNode) liChildren.elementAt(1).getChildren().elementAt(0).getChildren().elementAt(0);
String name = nameNode.getText();
// 4.4.获得软件的下载量
TextNode downloadNode = (TextNode) liChildren.elementAt(2).getChildren().elementAt(0);
String download = downloadNode.getText();
if (i != 0) {
sb.append(" , ");
}
sb.append("@{");
sb.append("@\"icon\" : @\"" + url + "\",");
sb.append("@\"name\" : @\"" + name + "\",");
sb.append("@\"download\" : @\"" + download + "\"");
sb.append("}");
}
sb.append("]");
System.out.println(sb);
/*
@[
@{
@"icon" : @"ggg.png",
@"name" : @"360手机卫士",
@"download" : @"65666次下载"
},
@{
@"icon" : @"ggg.png",
@"name" : @"360手机卫士",
@"download" : @"65666次下载"
},
@{
@"icon" : @"ggg.png",
@"name" : @"360手机卫士",
@"download" : @"65666次下载"
}
]*/
} catch (Exception e) {
e.printStackTrace();
}
}
}
H:/1217/02_plist填充到tableView_ViewController.m
// ViewController.m
// 360app,Java采集网页并生成json文件,再转成plist文件
// Created by apple on 13-12-17.
// Copyright (c) 2013年 itcast. All rights reserved.
#import "ViewController.h"
@interface ViewController ()
{
NSArray *_data;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 从mainBundel中加载plist生成字典数组
_data = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:@"360app.plist" ofType:nil]];
}
// 给旧的字典里面新增一对K V
- (void) addNewKeyValue
{
// 创建新的字典数组
NSMutableArray *array = [NSMutableArray array];
// 遍历旧的字典数组
for (int i = 0; i<_data.count; i++) {
// 旧字典数组中的旧字典
NSDictionary *oldDict = _data[i];
// 新空白字典
NSMutableDictionary *newDict = [NSMutableDictionary dictionary];
// 旧字典填充到新字典
[newDict setDictionary:oldDict];
// 新字典新增一对K V
[newDict setObject:[NSString stringWithFormat:@"%d.png", i]
forKey:@"local_icon"];
// 新字典数组中添加新字典
[array addObject:newDict];
}
// 将新字典数组写到Plist文件中,覆盖掉原来的旧plist文件
[array writeToFile:@"/Users/apple/Desktop/360app.plist" atomically:YES];
}
// 根据字典里面的Url,将图片全部下载到本地
- (void) downLoadPic
{
for (int i = 0; i<_data.count; i++) {
NSDictionary *app = _data[i];
NSData *imgData = [NSData dataWithContentsOfURL:
[NSURL URLWithString:app[@"picUrl"]]];
NSString *filename = [NSString stringWithFormat:@"/Users/apple/Desktop/360img/%d.png", i];
[imgData writeToFile:filename atomically:YES];
}
}
#pragma mark 每当有一个cell进入视野范围内就会调用,返回当前这行显示的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 0.用static修饰的局部变量,只会初始化一次
static NSString *ID = @"cell";
// 1.拿到一个标识先去缓存池中查找对应的Cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 2.如果缓存池中没有,才需要传入一个标识创建新的Cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
// 3.为每一行填充独一无二的数据
NSDictionary *app = _data[indexPath.row];
// 方式1:从网络加载图片,生成Data,再根据Data生成Image,但需要多线程,且耗时
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:app[@"icon"]]];
cell.imageView.image = [UIImage imageWithData:imgData];
// 方式2:从文件中读取图片名,生成imageView里面的Image,但不优雅
NSString *filename = [NSString stringWithFormat:@"%d.png", indexPath.row];
cell.imageView.image = [UIImage imageNamed:filename];
// 方式3:从字典中根据键取出图片名,阅读性好
cell.imageView.image = [UIImage imageNamed:app[@"local_icon"]];
cell.textLabel.text = app[@"name"];
cell.detailTextLabel.text = app[@"download"];
return cell;
}
#pragma mark 总共表格有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _data.count;
}
@end
H:/1217/03_ListView_MainActivity.java
package package cn.itcast.listview;
import java.util.ArrayList;
import java.util.HashMap;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 1.根据id获得内部的ListView
ListView listView = (ListView) findViewById(R.id.listview);
// 2.设置数据(适配器)ArrayList --- NSArray
// HashMap --- NSDictionary
ArrayList<HashMap<string object="">> data = new ArrayList<HashMap<string object="">>();
HashMap<string object=""> map1 = new HashMap<string object="">();
map1.put("image", R.drawable.girl_1);
map1.put("name", "林黛玉");
data.add(map1);
HashMap<string object=""> map2 = new HashMap<string object="">();
map2.put("image", R.drawable.girl_2);
map2.put("name", "史湘云");
data.add(map2);
/* 适配器构造方法
参数1:上下文
参数2:数据源
参数3:布局文件
参数4:from,字符串数组
参数5:to,控件在R文件中的数字id组成的数组
*/
SimpleAdapter adapter = new SimpleAdapter(
this,
data,
R.layout.list_item,
new String[]{"image", "name"},
new int[]{R.id.iamge, R.id.lable});
// 最后为listView设置适配器
listView.setAdapter(adapter);
}
}
</string></string></string></string></string></string>