以person 为例 下载人物图片 信息
创建一个person 对象
在.m 文件实现
NSCopying,NSCoding 的协议
创建一个personList 管理person 对象
实现里面的方法 其实可以把list 当做数组 来管理 person 对象
所以 重写了数组的一些基本方法;
然后我们再创建一个 db 类 管理list
创建db 是要管理list 所以从新写下上面的方法。可以更好的调用list 里面的方法;
基本上 数据模型已经出来了 我们要下载数据 还缺少一个 请求的一个类。
所以我们创建一个 httpHttpReques类
实现方法
http 请求方法;
#import "HttpReques.h"
@implementation HttpReques
-(void)creatWithString:(NSString *)urlStr withView:(NSInteger)viewId
{
self.viewId = viewId;
//定义URL
NSURL *url = [NSURL URLWithString:urlStr];
//创建网络请求
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
//创建连接
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
//开始连接
[connection start];
self.data = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"警告" message:@"error" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[al show];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.data appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//把下载完成的数据 传个引擎
// NSDictionary * personDic = [NSJSONSerialization JSONObjectWithData:_data options:NSJSONReadingAllowFragments error:nil];
//
//
// NSArray *arr =[personDic objectForKey:@"data"];
[self.dataDelegate ReloadData:_data withView:_viewId];
}
db =[[PersonDB alloc] init];
db.TableDelegate =self;
[db downLoadWithString:@"http://api.mengxiang.org/mxshow.php?mod=top&do=week" withView:110];