网络请求的三层架构

  以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;
  所以 需要2个协议
一个 下载数据 完成后给db
另一个  db处理完了数据 传个 控制器

在控制器 只需要 写三行代码 就可以实现下载

 db =[[PersonDB alloc] init];

    db.TableDelegate =self;

       [db downLoadWithString:@"http://api.mengxiang.org/mxshow.php?mod=top&do=week" withView:110];


 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值