// HttpDownload.h
#import <Foundation/Foundation.h>
#import "ASIHTTPRequest.h"
#import "HttpDownloadDelegate.h"
@interface HttpDownload : NSObject
<ASIHTTPRequestDelegate>
{
NSMutableData *mData;
id <HttpDownloadDelegate> _delegate;
}
- (void)downloadWithASIFromURL:(NSURL *)url;
@property (nonatomic,assign) id <HttpDownloadDelegate> delegate;
@property (nonatomic,readonly) NSMutableData *mData;
@end
// HttpDownload.m
#import "HttpDownload.h"
@implementation HttpDownload
@synthesize delegate,mData;
- (void)downloadWithASIFromURL:(NSURL *)url
{
mData = [[NSMutableData alloc] init];
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
request.delegate = self;
//启动异步下载
[request startAsynchronous];
}
#pragma mark - ASIHTTPRequestDelegate协议中的方法
//下载完成
- (void)requestFinished:(ASIHTTPRequest *)request
{
[mData appendData:[request responseData]];
//通知下载完成,解析并刷新界面
if ([delegate respondsToSelector:@selector(downloadCompleted:)]) {
[delegate downloadCompleted:self];
}
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSLog(@"下载失败");
}
@end
// HttpDownloadDelegate.h
#import <Foundation/Foundation.h>
@class HttpDownload;
@protocol HttpDownloadDelegate <NSObject>
//下载完成
- (void)downloadCompleted:(HttpDownload *)hd;
@end
// RootViewController.h
#import <UIKit/UIKit.h>
#import "HttpDownloadDelegate.h"
#import "HttpDownload.h"
@interface RootViewController : UIViewController
<HttpDownloadDelegate,UITableViewDelegate,UITableViewDataSource>
{
//显示数据
UITableView *mTableView;
//存放用户数据
NSMutableArray *dataArray;
HttpDownload *httpDownload;
}
@end
// RootViewController.m
#import "RootViewController.h"
#import "UserCell.h"
#import "UserItem.h"
#import "UIImageView+WebCache.h"
#import "GDataXMLNode.h"
@implementation RootViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//存放用户数据
dataArray = [[NSMutableArray alloc] initWithCapacity:0];
mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain];
mTableView.delegate = self;
mTableView.dataSource = self;
[self.view addSubview:mTableView];
[mTableView release];
httpDownload = [[HttpDownload alloc] init];
httpDownload.delegate = self;
NSURL *url = [NSURL URLWithString:@"http://192.168.88.8/sns/my/user_list.php?page=1&number=20&format=xml"];
[httpDownload downloadWithASIFromURL:url];
}
#pragma mark - UITableViewDatasource协议中的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dataArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellID = @"myCell";
UserCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"UserCell" owner:self options:nil] lastObject];
}
UserItem *item = [dataArray objectAtIndex:indexPath.row];
cell.usernameLabel.text = item.username;
cell.realnameLabel.text = item.realname;
cell.uidlabel.text = item.uid;
[cell.headimageView setImageWithURL:[NSURL URLWithString:item.headimage]];
return cell;
}
#pragma mark - UITableViewDelegate协议中的方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 114;
}
//下载完成,解析
- (void)downloadCompleted:(HttpDownload *)hd
{
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:hd.mData options:NSJSONReadingMutableContainers error:nil];
if (dict) {
for (NSDictionary *subDict in [dict objectForKey:@"users"]) {
UserItem *item = [[UserItem alloc] init];
item.username = [subDict objectForKey:@"username"];
item.realname = [subDict objectForKey:@"realname"];
item.uid = [subDict objectForKey:@"uid"];
item.headimage = [NSString stringWithFormat:@"http://192.168.88.8/sns%@",[subDict objectForKey:@"headimage"]];
[dataArray addObject:item];
[item release];
}
[mTableView reloadData];
}
}