iOS--关于同步下载

{

   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]]){
        //数组方法解析
    }
}

 

posted @ 2015-10-18 19:50  WheatDen  阅读(223)  评论(0编辑  收藏  举报