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