NSURLConnection下载类写在主视图控制器中

1.首先创建NSURLConnection类的实例

2.实现协议中的方法

3.分为三步:

收到服务器响应,要清空旧数据.

接收数据.

下载完成,采用iOS自带的NSJSONSerialization解析



//  RootViewController.h

#import <UIKit/UIKit.h>


@interface RootViewController : UIViewController

<UITableViewDelegate,UITableViewDataSource,NSURLConnectionDataDelegate>

{

    //显示加载的数据

    UITableView *mTableView;

    

    //连接下载类的实例

    NSURLConnection *mConnection;

    //用于接收下载的数据

    NSMutableData *mData;

    

    //用与存放用户数据的数组

    NSMutableArray *dataArray;

}

@end


//  RootViewController.m

#import "RootViewController.h"

#import "UserItem.h"


@implementation RootViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

    

    //下载相关

    

    NSURL *url = [NSURL URLWithString:@"http://192.168.88.8/sns/my/user_list.php?page=1&number=20"];

    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

    

    mConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    

    mData = [[NSMutableData alloc] initWithCapacity:0];

    

    //保存用户数据


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

}


#pragma mark - NSURLConnectionDataDelegate协议中的方法


//收到服务器响应

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{

    //首先要清空旧数据

    [mData setLength:0];

    NSLog(@"收到服务器响应........");

}


//接收数据,此方法会多次调用

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

    NSLog(@"接收数据........");

    [mData appendData:data];

}


//下载完成

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    NSLog(@"下载完成........");

    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:mData options:NSJSONReadingMutableContainers error:nil];

    

    NSArray *userArray = [dict objectForKey:@"users"];

    

    NSLog(@"userArray count = %d",[userArray count]);

    

    for (NSDictionary *dict in userArray) {

        UserItem *item = [[UserItem alloc] init];

        

        item.username = [dict objectForKey:@"username"];

        item.realname = [dict objectForKey:@"realname"];

        item.uid = [dict objectForKey:@"uid"];

        item.headimage = [NSString stringWithFormat:@"http://192.168.88.8/sns%@",[dict objectForKey:@"headimage"]];

        

        [dataArray addObject:item];

        [item release];

    }

    

    NSLog(@"dataArray count = %d",[dataArray count]);

    

    [mTableView reloadData];

}


- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

{

    NSLog(@"下载失败");

}


#pragma mark - UITableViewDatasource协议中的方法


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    

    return [dataArray count];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSString *cellID = @"myCell";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID] autorelease];

    }

    

    UserItem *item = [dataArray objectAtIndex:indexPath.row];

    

    cell.textLabel.text = item.username;

    return cell;

}


@end


//  UserItem.h

#import <Foundation/Foundation.h>


@interface UserItem : NSObject


@property (nonatomic,retain) NSString *username;

@property (nonatomic,retain) NSString *realname;

@property (nonatomic,retain) NSString *uid;

@property (nonatomic,retain) NSString *headimage;


@end


//  UserItem.m

#import "UserItem.h"


@implementation UserItem


@synthesize username,realname,uid,headimage;


@end


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值