码迷,mamicode.com
首页 > 其他好文 > 详细

UI---关于同步下载

时间:2015-10-18 21:21:30      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:

{

   NSMutableArray *_dataList;
}

[self loadDataWithPage:1];
    
[self loadDataWithURLConnection];

 

//记得初始化数组

_dataList =[NSMutableArray array];

 

 

-(void)loadDataWithURLConnection
{
    //创建URL对象
    NSURL *url =[NSURL URLWithString:@"http://iappfree.candou.com:8080/free/applications/limited//?currency=rmb&page=1"];
    //创建网络请求对象
    NSURLRequest  *request =[[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10];
    //同步请求数据
    NSHTTPURLResponse *response;
    NSData *data =[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
  //  NSLog(@"data=%@",data);
    //请求数据成功. code=200;
  //  NSLog(@"code =%li",response.statusCode);
    id Json =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    // NSLog(@"%@",Json);
    if ([Json isKindOfClass:[NSDictionary class]]) {
        //字典方法解析
        NSArray *applications =[Json objectForKey:@"applications"];
        NSLog(@"%@",applications);
        _dataList =[applications mutableCopy];
    }else if ([Json isKindOfClass:[NSArray class]]){
        //数组方法解析
    }
}
//从网络请求数据
-(void)loadDataWithPage:(NSInteger)pageIndex
{
    NSString *URLString =[NSString stringWithFormat:@"http://iappfree.candou.com:8080/free/applications/limited//?currency=rmb&page=%li",pageIndex];
    NSURL *url =[NSURL URLWithString:URLString];
    //读取网址内容为字符串对象
    NSString *dataString =[NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    //把字符串转换成NSData对象
    NSData *data =[dataString dataUsingEncoding:NSUTF8StringEncoding];
    //把直接把网址内容读取成NSData对象
   // NSData *data =[NSData dataWithContentsOfURL:url];
   // NSLog(@"%@",data);
    id Json =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
   // NSLog(@"%@",Json);
    if ([Json isKindOfClass:[NSDictionary class]]) {
        //字典方法解析
        NSArray *applications =[Json objectForKey:@"applications"];
       // NSLog(@"%@",applications);
        _dataList =[applications mutableCopy];
    }else if ([Json isKindOfClass:[NSArray class]]){
        //数组方法解析
    }
}

UI---关于同步下载

标签:

原文地址:http://www.cnblogs.com/LQCQ-Silent/p/4890117.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!