使用ASIHTTPRequest实现下载功能

//  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];

    }

    

}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值